On 2/11/2013 12:55 AM, David Hourn (DTH Design) wrote:
> char *getInputNoNewline(int max, char message[]) { static char* input; int
> maxchars = max+ADDCHARS
> input = (char*)malloc(maxchars); input = getInput(max, message); if
> (input[strlen(input) - 1] ==
> '\n') { input[strlen(input) - 1] = '\0'; } return input; }
Just looking at this code, I see these issues:
- You allocate space for input then overwrite the pointer to that space,
as other correspondents have noted.
- You call strlen more than once. Wasteful, though not *harmful*.
- You are counting on getInput to return a string that has at least
one character in it; if it has no characters then you will access
input[-1], which is out of range (and you might potentially damage
some other data structure, such as a header that malloc uses to
track items and to handle freeing).
If you expect no more than one \n in the string, then strchr may be
what you want, or strrchr to find the last \n regardless. If they
return non-null, you can examine the next byte to see if it is \0
and then clear the \n safely.
Regards -- Eliot Moss
------------------------------------------------------------------------------
Free Next-Gen Firewall Hardware Offer
Buy your Sophos next-gen firewall before the end March 2013
and get the hardware for free! Learn more.
http://p.sf.net/sfu/sophos-d2d-feb
_______________________________________________
Valgrind-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/valgrind-users