News for hackers. Stuff that matters.
Re:Parsing can become accidentally quadratic because of sscanf by fokinsean on Monday March 01, 2021 @12:20PM (#26305242)
> (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.

Re:Parsing can become accidentally quadratic because of sscanf by syrrim on Tuesday March 02, 2021 @12:20AM (#26312140)
I 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.

Re:Parsing can become accidentally quadratic because of sscanf by ben509 on Monday March 01, 2021 @01:29PM (#26306182)
Besides 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.

Re:Parsing can become accidentally quadratic because of sscanf by birktj on Monday March 01, 2021 @01:16PM (#26305981)
As 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.
Re:Parsing can become accidentally quadratic because of sscanf by wnoise on Monday March 01, 2021 @02:01PM (#26306681)
In 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.

Re:Parsing can become accidentally quadratic because of sscanf by ativzzz on Monday March 01, 2021 @01:06PM (#26305867)
It'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.

Re:Parsing can become accidentally quadratic because of sscanf by edflsafoiewq on Monday March 01, 2021 @04:24PM (#26308618)
Another 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.
Re:Parsing can become accidentally quadratic because of sscanf by TwoBit on Monday March 01, 2021 @02:09PM (#26306804)
The decision to use 0-terminated strings in C may be the single worst decision in programming language history, at least from a security standpoint.
Re:Parsing can become accidentally quadratic because of sscanf by perl4ever on Monday March 01, 2021 @07:57PM (#26310608)
"in-band signaling is the root of all evil" -anonymous