September 2nd, 2003, 05:29 PM
Buffer overflow protection
Here is my string function, it does the same as strcpy, I want to protect against buffer overflow. str1 will just be a pointer, and Ill malloc the space I need inside the function.
What's wrong with this?
void scpy(char *str1, char *str2)
//Copy string1 into string2
while(*str2 != '\0')
str1[x++] = *str2++;
str1[x] = '\0';
char s = "Automatically parse URLs";
September 2nd, 2003, 05:40 PM
I cannot see any obvious errors after a quick scan. Perhaps your slen() function has errors. Try posting the smallest amount of code you can that shows your problem, and we can take a look.
September 2nd, 2003, 06:12 PM
Note the extra parens :).
 To be absolutely bulletproof, I would check str1 after the malloc() to ensure that the value is not NULL. Also, I would change the function type to return int instead of void and make it return success or failure this way. [/edit]
[edit2] Just noticed that you're passing address of g from main() and trying to allocate g within scpy. In this case, your argument type for the first argument is incorrect, since you're passing the address of a char ptr variable. Oh, and man strdup() to save yourself the trouble of reinventing the wheel :)[/edit2]
Last edited by Scorpions4ever; September 2nd, 2003 at 07:05 PM.
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
"I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo