Timeline for Since `strcpy`, `strcat`, and `sprintf` are dangerous, what shall we use in stead of them?
Current License: CC BY-SA 4.0
9 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Oct 27, 2020 at 18:03 | comment | added | gnasher729 | That’s why you shouldn’t use fixed length buffers at all. Strncpy doesn’t solve the problem. | |
Oct 26, 2020 at 10:40 | comment | added | IMSoP | @gnasher729 Sure, but if you can't calculate the correct length for your buffer, you're going to have those problems whatever string functions you use. In fact, you'll likely have both a buffer overflow and corrupted UTF-8, because something else will write over the part of your string that's outside your expected length. I'm also struggling to think of a situation where you'd know the number of code points in a UTF-8 string, but not the number of bytes it takes up, unless you're explicitly converting from some other encoding. | |
Oct 25, 2020 at 13:18 | comment | added | gnasher729 | Imsop Says who? Corrupted UTF-8 strings are highly dangerous and can cause all kinds of things going wrong. And fixing one problem by opening up two others is not a good idea. A strcpy that doesn’t copy a string or a strcat that doesn’t concatenate a string are worse than useless. | |
Oct 25, 2020 at 8:55 | comment | added | Deduplicator |
Do you actually know that strncpy() is the only one of them which does not leave a nul-terminated string if the input is long enough, and it always writes the whole buffer, as it is the only one that is not a string-function with a length? If you do, warn about it, there are already far too many who don't.
|
|
Oct 25, 2020 at 4:25 | comment | added | cwallach | The original post was worried about destination buffer overflows which the strN functions handle, except for the string termination problem. | |
Oct 24, 2020 at 23:37 | comment | added | Deduplicator | One of those is not like the others: It doesn't actually work on nul-terminated strings. Not knowing that will lead to interesting effects. | |
Oct 24, 2020 at 21:57 | comment | added | IMSoP | @gnasher729 If your text is longer than the buffer length you've calculated, it's better to have a corrupted UTF-8 code point than a buffer overflow! | |
Oct 24, 2020 at 21:47 | comment | added | gnasher729 | strncpy etc. are totally unusable if you use UTF-8, which is nowadays most common, because it can cut off in the middle of a code point, leaving you with an invalid string. The much bigger problem is that it may prevent a buffer overflow, but you don't get a copy of the input string, or a concatenation of the input strings. | |
Oct 24, 2020 at 18:53 | history | answered | cwallach | CC BY-SA 4.0 |