3 ms·
Saying the "only advantage" is space efficiency is definitely weird. Null-termination means that a string can't contain \0, which has both good and bad conseque
by theoh 8y ago
Saying the "only advantage" is space efficiency is definitely weird. Null-termination means that a string can't contain \0, which has both good and bad consequences. Maybe what the author means is that the technique of just running through the string until you hit a null has been an unmitigated disaster from a security perspective. (Buffer overflows often result from a strcpy() which receives a longer string than expected as an argument.)
See this proposal which takes that problem as its starting point: https://cr.yp.to/proto/netstrings.txt https://cr.yp.to/proto/netstrings.txt
More generally, the implication that space efficiency is the justification for null-termination is probably not historically true. It was a form of in-band signalling, just like getchar() returning EOF.
When null-termination is used in network protocols, the data to be sent has to be prepared in order to remove/escape any nulls. This process is called byte stuffing, and naturally it could result in serious inefficiency if a long string of nulls occurred in the input data: https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuff...