3 ms·
> (4) Null-terminated strings are the definition of insanity Could you elaborate on this one? I've only used C in college and don't know why this is bad.
by fokinsean 6y ago
> (4) Null-terminated strings are the definition of insanity
Could you elaborate on this one? I've only used C in college and don't know why this is bad.
- ativzzz 6y agoIt's incredibly easy to do something wrong and cause bugs or security issues in your app. Or, compare string manipulation in C with any high level language like Ruby, Python, JS and the ease of use is night and day.
- birktj 6y agoAs is the case in this article it often happens that having the length of a string is required for certain operations, which leads to a lot of calls to strlen. This doesn't really seem like such a big problem to begin with as one assumes that the operations that require the length also have linear complexity. However it turns out (like in the article) that this is not always the case which leads to unexpected quadratic algorithms. The pointer + length representation is only 7 bytes longer on modern architectures and does not have this problem and has a few other advantages like being able to create substrings without allocating new memory.
- wnoise 6y agoIn fact, having the length of the string was not required. Certain ways of implementing scanf() required getting the length of the string, or otherwise scanning the whole thing. But saner ways did not.
- ben509 6y agoBesides the fact that you have to walk the string just to find the length of it... Null-terminated strings try to save space by encoding the string length by convention. This can fail due to off-by-one errors, mistakenly allocating a fixed buffer, strings that have an unexpected null in them, and more. Those can lead to all kinds of nasty problems like buffer overflows, which allow someone who can craft an input to write arbitrary stuff into memory. God only knows how many security vulnerabilities and performance problems could be traced back to null terminated strings.
- TwoBit 6y agoThe decision to use 0-terminated strings in C may be the single worst decision in programming language history, at least from a security standpoint.
- perl4ever 6y ago"in-band signaling is the root of all evil" -anonymous
- edflsafoiewq 6y agoAnother reason is a substring of a nul-terminated string is only nul-terminated if it is a suffix. So slicing out a substring generally requires copying to a new buffer so you can append the nul byte.
- syrrim 6y agoI would point out two main issues: 1. Such a string cannot contain arbitrary bytes. Since it is frequently useful to work with arrays of arbitrary bytes, we have to repeat every library function twice, once for strings that can contain nuls (the mem* series of functions), and once for strings terminated by nuls (the str* series of functions). The latter are much better supported, and are easier to work with, so we frequently see programs that trip over strings with nuls in them for no particularly good reason. 2. We often want to modify strings, not just read them. Modifications often need more space than was in the original string. A nul terminated string cannot say how much room beyond the current length is allowed to be written to. By default, most library functions assume that there is enough space, however this is frequently an unsafe assumption. This again causes us to duplicate every library function twice over in order to specify the size of the buffer (the strn* series of functions). Since, again, these functions are poorly supported and harder to use, we see many many instances of programs that have buffer overflow bugs, and therefore likely RCEs, for no good reason at all.