Oddbean new post about | logout
 Yeah, I simply avoid them. I used macros and tiny structs to track length. In noscrypt I replicated C# spans with inline functions and small structs, the same way literally every other language does because most of them are written in C anyway lol. 

To be fair though, if people didn't keep ignoring the language requirements, ub, and ignoring compiler manuals, storing the termination with 0 at the end of a buffer is arguably the most efficient way to handle that. Strings are just a sequence of memory, and literals are stored in data segments. Most efficient way to handle that imo. 

https://git.vaughnnugent.com/cgit/vnuge/noscrypt/.git/tree/src/nc-util.h#n96 
 > I used macros and tiny structs to track length. In noscrypt I replicated C# spans with inline functions and small structs, the same way literally every other language does because most of them are written in C anyway lol. 

the way you say that, C is mostly a language for people with too much time on their hands 😀 it's great if things like that just work with the standard types provided by the compiler/standard library, believe me, after 30 years of coding it doesn't sound like fun anymore to roll it yourself

> storing the termination with 0 at the end of a buffer is arguably the most efficient way to handle that

nah, PASCAL strings (storing the length explicitly) are slightly more efficient in most cases, as there's no need to iterate over the string again and again to get the length.

But their biggest problem isn't efficiency, it's that a character (\0) is special-cased. This can often be exploited in various ways in protocol handling.

(i say this as someone that has found tons of remote exploitable bugs in C code, including libminiupnp 😱 ) 
 length prefixes is how i do all of my string and other array encoding in my binary data protocols

Wurth was right about this, and notably, pretty sure that if you asked Thompson he would say that null termination was a false economy