473,756 Members | 6,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

memory leak purify

Hello,

I've writen one simple program to automate some manual process. I've
written that in c program. It works fine so far no problem reported on
this. Last week, i get chance to run my program on purify tool. I'm
very new to purify tool use.

Q1. Purify reports Uninitialized memory read in the following line A
and Line B

<<<<<<<<<<<<<<< <<<<<<<<<<<<<>> >>>>>>>>>>>>>>> >>>>>>>
int parameter_parse (char *ptr)
{
char *tmp;
PARAM *p_tmp;
p_tmp = (struct PARAM *)malloc(sizeof (struct PARAM));
if (!( tmp = strchr(ptr, '=') ))
printf("\nBaili ng out..");
exit (1);
}
tmp++; // since ptr includes delimiters too ("=")

p_tmp->value = strdup(tmp);
p_tmp->field =
(char *)malloc(strlen (ptr)-strlen(p_tmp->value)); // Line A
strncat(p_tmp->field, ptr,
(strlen(ptr)-strlen(p_tmp->value)-1)); // Line B
p_tmp->next = NULL;

if ( p_start == NULL)
{
p_start = p_tmp;
p_last = p_tmp;
} else {
p_last->next = p_tmp;
p_last = p_tmp;
}
return 0;
}
<<<<<<<<<<<<<<< <<<<<<<<<<<<<>> >>>>>>>>>>>>>>> >>>>>>>>
This "parameter_pars e" funciton receives input in this form
"key=value" . I dont know wht is the "Uninitiali zed memory read" memory
read at these two lines.

Q2. Similarly, it shows UMR in the following two lines
if ( t == NUMERIC )
strcat(res, NUMBER[9]); // Line C
else
strcat(res, ALPHABETS[9]); // Line D

Here, NUMBER and ALPHABETS are an array of 10 pointers-to-int.

Please help me to resolve this problem.
Thanks in advance.

Wluve,
Sangeetha.

Nov 14 '05 #1
6 2553
sa*********@ind ia.com wrote:
int parameter_parse (char *ptr)
{
char *tmp;
PARAM *p_tmp;
p_tmp = (struct PARAM *)malloc(sizeof (struct PARAM));
if (!( tmp = strchr(ptr, '=') ))
printf("\nBaili ng out..");
exit (1);
} You do realise that this curly brace is the end of the function
parameter_parse (), don't you? You compiler *should* complain about a
"statement outside of any function" on the next line. If, however, your
program compiles and runs, make sure you have copied and pasted (or
imported into your mailer) the *actual code*.
tmp++; // since ptr includes delimiters too ("=")

p_tmp->value = strdup(tmp);
p_tmp->field =
(char *)malloc(strlen (ptr)-strlen(p_tmp->value)); // Line A
strncat(p_tmp->field, ptr,
(strlen(ptr)-strlen(p_tmp->value)-1)); // Line B
p_tmp->next = NULL;

if ( p_start == NULL)
{
p_start = p_tmp;
p_last = p_tmp;
} else {
p_last->next = p_tmp;
p_last = p_tmp;
}
return 0;
}

Presumably, that is where you *intended* the function to end.
If you run your code through an indenter, it would pick out the problem
immediately, and make your code considerably more legible (hint, hint).

--
Simon Richard Clarkstone: s.************@ durham.ac.uk/s*m*n.cl*rkst*n *@
hotmail.com ### "I have a spelling chequer / it came with my PC /
it plainly marks for my revue / Mistake's I cannot sea" ...
by: John Brophy (at: http://www.cfwf.ca/farmj/fjjun96/)
Nov 14 '05 #2
On 6 Dec 2004 23:55:33 -0800
sa*********@ind ia.com wrote:
Hello,

I've writen one simple program to automate some manual process. I've
written that in c program. It works fine so far no problem reported on
this. Last week, i get chance to run my program on purify tool. I'm
very new to purify tool use.

Q1. Purify reports Uninitialized memory read in the following line A
and Line B
firstly, you need to get the code you post decently formatted using
spaces (not tabs) for indenting. I have reformatted your code below
since I need to in order to read it, but other than that the code marked
as quotation is unchanged.
<<<<<<<<<<<<<<< <<<<<<<<<<<<<>> >>>>>>>>>>>>>>> >>>>>>>
int parameter_parse (char *ptr)
{
char *tmp;
PARAM *p_tmp;
p_tmp = (struct PARAM *)malloc(sizeof (struct PARAM));
You don't need to cast the return value of malloc and it can hide the
very real error of failing to 'include <stdlib.h>. a far simpler and
less error prone form it:

p_tmp = malloc(sizeof *p_tmp);

if (!( tmp = strchr(ptr, '=') ))
{ /* otherwise it will always exit */
printf("\nBaili ng out..");
exit (1);
exit(1) is not portable. The only portable values are 0, EXIT_SUCCESS
and EXIT_FAILURE. So for portability this should be
exit(EXIT_FAILU RE);
However, I accept that sometimes there are valid reasons for using
non-portable values.

Also, I think you retyped your code rather than copying and pasting,
since as written here this function will *always* call exit. Therefore
the error you are actually looking for is not exhibited by this code and
even after fixing this there could be other differences that are
relevant.

Always use copy and paste, *never* retype code for posting to usenet.
}
tmp++; // since ptr includes delimiters too ("=")

p_tmp->value = strdup(tmp);
strdup is a non-standard function. I happen to know what it does, but
you can't assume everyone here does and being non-standard it is off
topic here.
p_tmp->field =
(char *)malloc(strlen (ptr)-strlen(p_tmp->value)); // Line A
same comments about casting as in the previous example.
p_tmp->field = malloc(strlen(p tr)-strlen(p_tmp->value));

I can't see anything obviously wrong with this. However, it could be a
problem with the data passed in or you not typing what your actual code
is.
strncat(p_tmp->field, ptr,
(strlen(ptr)-strlen(p_tmp->value)-1)); // Line B
This, on the other hand, is definitely a problem. p_tmp->field points to
*uninitialised* memory and strncat obviously has to read the memory in
order to find the end of the string. This could make things go BANG big
time. However, since you know how much there is to copy (you worked it
out in order to allocate the memory) why not just use memcpy then set
the null termination on the string?
p_tmp->next = NULL;

if ( p_start == NULL)
{
p_start = p_tmp;
p_last = p_tmp;
} else {
p_last->next = p_tmp;
p_last = p_tmp;
}
return 0;
}
<<<<<<<<<<<<<<< <<<<<<<<<<<<<>> >>>>>>>>>>>>>>> >>>>>>>>
This "parameter_pars e" funciton receives input in this form
"key=value" . I dont know wht is the "Uninitiali zed memory read" memory
read at these two lines.

Q2. Similarly, it shows UMR in the following two lines
if ( t == NUMERIC )
strcat(res, NUMBER[9]); // Line C
else
strcat(res, ALPHABETS[9]); // Line D

Here, NUMBER and ALPHABETS are an array of 10 pointers-to-int.

Please help me to resolve this problem.
Thanks in advance.


You probably have not initialised res. If you look up the definition of
strcat you will see that the destination buffer obviously requires
initialising.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #3
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
On 6 Dec 2004 23:55:33 -0800
sa*********@ind ia.com wrote:
Q2. Similarly, it shows UMR in the following two lines
if ( t == NUMERIC )
strcat(res, NUMBER[9]); // Line C
else
strcat(res, ALPHABETS[9]); // Line D

Here, NUMBER and ALPHABETS are an array of 10 pointers-to-int. ^^^^^^^^^^^^^^^
Please help me to resolve this problem.
Thanks in advance.
You probably have not initialised res. If you look up the definition of
strcat you will see that the destination buffer obviously requires
initialising.


And, of course, using an int pointer as an argument to a string
handling function like strcat() isn't a good idea to start with.
If you want to convert an integer to a string use e.g. sprintf().

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #4
<sa*********@in dia.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
Hello,

I've writen one simple program to automate some manual process. I've
written that in c program. It works fine so far no problem reported on
this. Last week, i get chance to run my program on purify tool. I'm
very new to purify tool use.
/* The code is not well formed, indentation should be consistent
* and only using spaces.
*/

/* Missing declarations here: */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct PARAM PARAM;
struct PARAM {
PARAM *next;
char *field;
char *value;
};

PARAM *p_start, *p_last;
int parameter_parse (char *ptr)
since parameter_parse does not modify its input buffer, define it as const
{
char *tmp;
PARAM *p_tmp;
poor choice of names. no information carried.
p_tmp = (struct PARAM *)malloc(sizeof (struct PARAM));
useless cast. error prone even.
allocation failure not tested.
if (!( tmp = strchr(ptr, '=') ))
missing {
this code cannot compile as is.
bad style.
printf("\nBaili ng out..");
uninformative error message.
exit (1);
unportable exit() parameter value
}
tmp++; // since ptr includes delimiters too ("=")
do not use // comments
the comment itself is awkward, just say that tmp points to '='
with a short notice like skip '='
p_tmp->value = strdup(tmp);
non standard function, but a fine addition ;-)
no test for memory allocation failure.
p_tmp->field =
(char *)malloc(strlen (ptr)-strlen(p_tmp->value)); // Line A
useless cast, allocation too short by 1 byte !
it beats me why other forum regulars do not catch this!
cumbersome and slow computation of "field" length.
strncat(p_tmp->field, ptr,
(strlen(ptr)-strlen(p_tmp->value)-1)); // Line B
redundant computation of "field" length.
strncat() to an uninitialized buffer.
one more example of strncat() misuse. function should be deprecated.
p_tmp->next = NULL;

if ( p_start == NULL)
{
p_start = p_tmp;
p_last = p_tmp;
} else {
p_last->next = p_tmp;
p_last = p_tmp;
}
inconsistent style
return 0;
constant return value.
Why not return an error code instead of "bailing out" ?
}


---------------

/* Here is an example with most issues fixed: */

int parameter_parse (const char *ptr)
{
char *sep;
PARAM *p_param;

p_param = malloc(sizeof(* p_param));
if (!p_param) {
fprintf(stderr, "cannot allocate memory for PARAM structure\n");
exit(EXIT_FAILU RE);
}
if ((sep = strchr(ptr, '=')) == NULL) {
fprintf(stderr, "invalid parameter line: %s\n", ptr);
exit(EXIT_FAILU RE);
}

p_param->field = calloc(sep - ptr + 1, sizeof(char));
p_param->value = calloc(strlen(s ep + 1) + 1, sizeof(char));
p_param->next = NULL;

if (!p_param->field || !p_param->value) {
fprintf(stderr, "cannot allocate memory for PARAM members\n");
free(p_param->field);
free(p_param->value);
free(p_param);
exit(EXIT_FAILU RE);
}

memcpy(p_param->field, ptr, sep - ptr);
strcpy(p_param->value, sep + 1);

if (p_start == NULL) {
p_start = p_last = p_param;
} else {
p_last->next = p_param;
p_last = p_param;
}
return 0;
}

--
Chqrlie

Nov 14 '05 #5
thanks a lot for your help

Charlie Gordon wrote:
<sa*********@in dia.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
Hello,

I've writen one simple program to automate some manual process. I've written that in c program. It works fine so far no problem reported on this. Last week, i get chance to run my program on purify tool. I'm
very new to purify tool use.
/* The code is not well formed, indentation should be consistent
* and only using spaces.
*/

/* Missing declarations here: */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct PARAM PARAM;
struct PARAM {
PARAM *next;
char *field;
char *value;
};

PARAM *p_start, *p_last;
int parameter_parse (char *ptr)


since parameter_parse does not modify its input buffer, define it as

const
{
char *tmp;
PARAM *p_tmp;
poor choice of names. no information carried.
p_tmp = (struct PARAM *)malloc(sizeof (struct PARAM));


useless cast. error prone even.
allocation failure not tested.
if (!( tmp = strchr(ptr, '=') ))


missing {
this code cannot compile as is.
bad style.
printf("\nBaili ng out..");


uninformative error message.
exit (1);


unportable exit() parameter value
}
tmp++; // since ptr includes delimiters too ("=")


do not use // comments
the comment itself is awkward, just say that tmp points to '='
with a short notice like skip '='
p_tmp->value = strdup(tmp);


non standard function, but a fine addition ;-)
no test for memory allocation failure.
p_tmp->field =
(char *)malloc(strlen (ptr)-strlen(p_tmp->value)); // Line A


useless cast, allocation too short by 1 byte !
it beats me why other forum regulars do not catch this!
cumbersome and slow computation of "field" length.
strncat(p_tmp->field, ptr,
(strlen(ptr)-strlen(p_tmp->value)-1)); // Line B


redundant computation of "field" length.
strncat() to an uninitialized buffer.
one more example of strncat() misuse. function should be deprecated.
p_tmp->next = NULL;

if ( p_start == NULL)
{
p_start = p_tmp;
p_last = p_tmp;
} else {
p_last->next = p_tmp;
p_last = p_tmp;
}


inconsistent style
return 0;


constant return value.
Why not return an error code instead of "bailing out" ?
}


---------------

/* Here is an example with most issues fixed: */

int parameter_parse (const char *ptr)
{
char *sep;
PARAM *p_param;

p_param = malloc(sizeof(* p_param));
if (!p_param) {
fprintf(stderr, "cannot allocate memory for PARAM

structure\n"); exit(EXIT_FAILU RE);
}
if ((sep = strchr(ptr, '=')) == NULL) {
fprintf(stderr, "invalid parameter line: %s\n", ptr);
exit(EXIT_FAILU RE);
}

p_param->field = calloc(sep - ptr + 1, sizeof(char));
p_param->value = calloc(strlen(s ep + 1) + 1, sizeof(char));
p_param->next = NULL;

if (!p_param->field || !p_param->value) {
fprintf(stderr, "cannot allocate memory for PARAM members\n"); free(p_param->field);
free(p_param->value);
free(p_param);
exit(EXIT_FAILU RE);
}

memcpy(p_param->field, ptr, sep - ptr);
strcpy(p_param->value, sep + 1);

if (p_start == NULL) {
p_start = p_last = p_param;
} else {
p_last->next = p_param;
p_last = p_param;
}
return 0;
}

--
Chqrlie


Nov 14 '05 #6
sa*********@ind ia.com wrote:

thanks a lot for your help


I have my doubts that you will be getting much more, after this
silly and rude top-posting and failure to snip 160 odd lines that
have no connection to your posting.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1685
by: John Black | last post by:
Hi, In debugging a core dump, I run purify against my code, it says several "Free Memory Read" errors. All the errors are on one delete statement. Usually this means some pointer is deleted by non-owner. But it is really hard to locate the error code. The code structure is like this: class BigClass{ // this is from somebody else code ......
2
4958
by: Generic Usenet Account | last post by:
I have been using STL for a long time now, without any problems. Recently we generated a purification report on our software using Rational Purify, and we found some memory leaks. My colleague claims that some of the memory leaks are due to the fact that "STL is wrought with memory leaks". Of course I disagree. I think that there are no "inherent leaks" with STL, but if used improperly, leaks will occur. One common source of leak that...
4
1710
by: Billy Patton | last post by:
I have this test code: { String s("123"); OK(s.Len() == 3); OK is a define to print __LINE__ and increment a test_count and a failure count String is built using std::string as the memory namager for the string.
2
1555
by: Grahamo | last post by:
Hi, I'm hoping to get some feedback from users regarding heap analysis tools available (apart from purify) for leak tracking. I'm working with a large, already deployed codebase that needs fixing. the problem is that the compiler in question (borland c++ on windows) doesn't support purify which is my first choice. I'd like to know what the best alternatives are. I've heard of boundschecker, smartheap and such like. Before I try any of...
10
14053
by: eyh5 | last post by:
Hi, My C code (running on Soalris Unix) has some "segmentation fault" that I wish to use purify to do it. I poked around the web, and found some information about adding some lines in a Makefile file to use purify. However, my code is a rather simple single-source C program, and I didn't write a Makefile for it. I'm wondering if anybody can tell me which commands are to be entered at the Unix prompt to use purify. And, I don't know if...
1
2074
by: Lighter | last post by:
Is there a way to write a memory leak detector supporting new(nothrow)? For example, #include <My_Debug_New.h> using namespace std; int main() {
5
3741
by: nithya4u | last post by:
I am working on a c++ module, where large amount of data needs to be written to a stream and str() method is been used to assign the value of this to the char*. This produces the intended result. But when i run purify on it i get the following results. MLK: Memory leak of 8656 bytes from 9 blocks allocated in strstreambuf::doallocate(void) Distribution of leaked blocks 987 bytes from 1 block of 987 bytes (0x03977ea8) 980 bytes from 1...
17
2546
by: Mike | last post by:
Hello, I have following existing code. And there is memory leak. Anyone know how to get ride of it? function foo has been used in thousands places, the signature is not allowed to change. Thanks in advance,
9
4216
by: jeungster | last post by:
Hello, I'm trying to track down a memory issue with a C++ application that I'm working on: In a nutshell, the resident memory usage of my program continues to grow as the program runs. It starts off at a nice 4% of memory, then slowly grows up to 50% and beyond. This translates to around 2 gigs of physical memory, and that's really way more memory than this program should be taking up.
0
9462
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9287
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9886
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9857
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7259
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6542
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.