468,727 Members | 1,601 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,727 developers. It's quick & easy.

malloc->free->damage: after normal block

Below is the code which was written VC++ 6.0 under windows environment.

Executing the same throws:
------------------
Debug Error!
Program: ccheck.exe
DAMAGE: after normal block (#41) at 0x00300160

(Press Retry to debug the application)
------------------
While the free () statement is commented the program does not report the error.

Please do let me know your views ...

Thanks in advance.

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

# define _DEBUG_ 1

void SetProgramName (char *);
char * GetProgramName ();
void PrintUsage ();
void PrintErrMsg (size_t);
void ClearMemory ();

char *ProgramName = NULL;

extern errno;

int
main (int argc, char *argv[])
{
SetProgramName (argv[0]);

if (argc < 2)
{
PrintUsage ();
ClearMemory ();
}

return (EXIT_SUCCESS);
}

void
SetProgramName (char *PrgName)
{

ProgramName = (char *) malloc (strlen (PrgName));

if (ProgramName == NULL)
{
PrintErrMsg (errno);
}
else
{
(void) strcpy (ProgramName, PrgName);

#if defined (_DEBUG_)
(void) fprintf (stderr, "\n Debug: ProgramName %s \n", ProgramName);
#endif

}
}

char *
GetProgramName ()
{
return (ProgramName == NULL ? NULL : ProgramName);
}

void
ClearMemory ()
{
if (strlen (ProgramName) > 0)
{
free (ProgramName);
#if 0
ProgramName = NULL;
#endif
PrintErrMsg (errno);
}
}
void
PrintErrMsg (size_t ErrNumber)
{
(void) fprintf ( stderr, \
"\n Err Number [%ld] \n Err Msg [%s] \n", \
ErrNumber, strerror (ErrNumber)
);
}

void
PrintUsage ()
{
(void) fprintf (stderr, "\n %s <> <>", GetProgramName());
}
---

Thanks,
Anu
Nov 14 '05 #1
6 5313
boa
Anuradha wrote:
Below is the code which was written VC++ 6.0 under windows environment.

Executing the same throws:
------------------
Debug Error!
Program: ccheck.exe
DAMAGE: after normal block (#41) at 0x00300160

(Press Retry to debug the application)
------------------
While the free () statement is commented the program does not report the error.

Please do let me know your views ... [snip]
void
SetProgramName (char *PrgName)
{

ProgramName = (char *) malloc (strlen (PrgName));

ProgramName = malloc(strlen(PrgName) + 1);
boa

[snip]
Nov 14 '05 #2
> ProgramName = (char *) malloc (strlen (PrgName));

You are not allocating enough memory here.

if (ProgramName == NULL)
{
PrintErrMsg (errno);
}
else
{
(void) strcpy (ProgramName, PrgName);


And here you run over memory beyond what you allocated.

Gordon L. Burditt
Nov 14 '05 #3
Anuradha wrote:
# include <string.h>
# include <stdio.h>
# include <stdlib.h>
# include <errno.h>

# define _DEBUG_ 1

void SetProgramName (char *);
char * GetProgramName ();
void PrintUsage ();
Better:
char * GetProgramName (void);
void PrintUsage (void);
void PrintErrMsg (size_t);
void ClearMemory ();
Same here.
char *ProgramName = NULL;

extern errno;
It would be a good idea to tell your program about the type of errno.
int
main (int argc, char *argv[])
{
SetProgramName (argv[0]);

if (argc < 2)
{
PrintUsage ();
ClearMemory ();
}

return (EXIT_SUCCESS);
}

void
SetProgramName (char *PrgName)
{

ProgramName = (char *) malloc (strlen (PrgName));
You need
ProgramName = malloc (strlen (PrgName) + 1);
because the '\0' character is not counted by strlen().

if (ProgramName == NULL)
{
PrintErrMsg (errno);
This is kind of silly, since errno is a global variable anyway.
}
else
{
(void) strcpy (ProgramName, PrgName);

#if defined (_DEBUG_)
(void) fprintf (stderr, "\n Debug: ProgramName %s \n", ProgramName);
#endif

}
}

char *
GetProgramName ()
{
return (ProgramName == NULL ? NULL : ProgramName);
This is equivalent to
return ProgramName;
}

void
ClearMemory ()
{
if (strlen (ProgramName) > 0)
This is not the way to test whether the string allocation was successful.
You want
if (ProgramName != NULL)
{
free (ProgramName);
#if 0
ProgramName = NULL;
#endif
PrintErrMsg (errno);
}
}


By the way: You don't need to copy argv[0] at all, since it exists until the
end of the program.
Christian
Nov 14 '05 #4
On Thu, 26 Aug 2004 08:53:01 +0200
Christian Kandeler <ch****************@hob.de> wrote:
Anuradha wrote:
# include <string.h>
# include <stdio.h>
# include <stdlib.h>
# include <errno.h>
<snip>
extern errno;


It would be a good idea to tell your program about the type of errno.


No it isn't, it's better to delete the "extern errno;" entirely. errno
is declared appropriately by errno.h

<snip>
if (ProgramName == NULL)
{
PrintErrMsg (errno);


This is kind of silly, since errno is a global variable anyway.


It is silly.

However, errno might not be a global variable, it could be a macro that
expands to a modifiable lvalue :-)
--
Flash Gordon
Pedantic to the last, except I sometimes get it wrong.
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #5
>>
if (ProgramName == NULL)
{
PrintErrMsg (errno);


This is kind of silly, since errno is a global variable anyway.


No, it's *NOT* silly. It's perfectly possible and reasonable to
pass something else (for example, a saved version of errno from
before you tried opening the log file to put the error message in).
Being forced to save and restore errno is a pain, and so many things
MIGHT mess it up (such as opening log files, and even printf()),
it's often necessary. With a function taking an argument, you're
still sometimes stuck with saving errno, but usually not with
restoring it.

It's not that unusual to have a function return an error code or 0
to indicate success. I see that a lot inside kernels. There, you
want to deal with what the kernel returned, not errno.

Why does strerror() take an argument rather than use errno? Same
issue.

Gordon L. Burditt
Nov 14 '05 #6
Anuradha wrote:
return (ProgramName == NULL ? NULL : ProgramName);

return ProgramName;

Not an error, though... See the other replys for that.

-- Thomas
Nov 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Hongzheng Wang | last post: by
74 posts views Thread by Suyog_Linux | last post: by
42 posts views Thread by Joris Adriaenssens | last post: by
41 posts views Thread by jacob navia | last post: by
7 posts views Thread by Louis B. (ldb) | last post: by
17 posts views Thread by anyone.anon | last post: by
71 posts views Thread by desktop | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.