Re:Massacring C Pointers
by konstmonst on Tuesday June 26, 2018 @02:19AM
(#17398294)
There are multiple problems with that code:
1) return(r) He is returning pointer to temporary! When you declare and initialize variables on stack after that function, they will overwrite memory pointed by r.
2) He is assuming size of string pointed by s(including 0) is less than 100 and also the combined sizes of s and t are less than 100. Stack Overflow!
3) Not an errors, but would not pass my code review: Inconsistent Variable declaration, using signed integer for loop where you don't need signed.
Also I would have used strcpy 2 times, or loop 2 times, not two different ways. Beside that, you should not use strcpy, but strncpy to avoid stack overflows.
[ Parent
| on HN ]
> Inconsistent Variable declaration
How so?
> I would have used strcpy 2 times
You can't do this, because strcpy doesn't give you the length of the string you copied, which is necessary to put the trailing null byte.
strcpy writes a trailing zero.
> The strcpy() function copies the string pointed to by src, including the terminating null byte ('\0'), to the buffer pointed to by dest.