5 ms·
I've got a better code snippet: your exact same code snippet but for a language which will short circuit based on the lvalue of the assignment expression. Then
by jancsika 7y ago
I've got a better code snippet: your exact same code snippet but for a language which will short circuit based on the lvalue of the assignment expression.
Then t would never be able to overflow s (nor even eat its null terminator)
- jjnoakes 7y agoThe size of the buffer pointed to by s may be larger than the current string it holds. It may also be uninitialized.
- saagarjha 7y agoOr point to the wrong thing, or point to unmapped memory, or be const, or…there's a lot of things that can go wrong with this function.
- jjnoakes 7y agoThe things I listed are things the function does right as written but could not do if it worked the way suggested above. The things you listed are general concerns in C which are unrelated to both the correct implementation and the suggested changes.
- dhsysusbsjsi 7y agoThat’s what strncpy() is for