Mail Archives: djgpp-workers/1998/08/23/09:24:33
On Sun, 23 Aug 1998, Robert Hoehne wrote:
> I don't know if the current behaviour is correct, but after
> looking in the sources, I found, that strncpy copies
> _ALWAYS_ as many characters as given in the third argument.
This is ANSI requirement: strncpy should pad with zeroes if the second
argument is shorter than what the third specifies.
> Here we allocating memory with strlen(fc)+1 bytes. Let's say,
> the fc is "foo.exe", then we allocating 8 bytes.
>
> strncpy(prog_name, fc, 16);
>
> Now we copying 16! bytes to progname.
Then why not just calloc(17)? Or, better yet, use strncat?
> And an other hint for Eli: Maybe it's a good idea to include
> somewhere in the FAQ the trick I did above with the bsdmallo.c
> (compiling with -DTEST -DRCHECK) since it seems to me good
> technique to find memory corruptions in a program since it
> will abort the program if you free a pointer which was illegal
> overwriten (the memory it points to).
Thanks, I will keep that in mind.
- Raw text -