3 ms·
Pointer and length representations are available in C, and we can find programs that get them wrong somewhere. "Such and such a data structure ... in the middl
by kazinator 7y ago
Pointer and length representations are available in C, and we can find programs that get them wrong somewhere.
"Such and such a data structure ... in the middle of Rust" is not a discussion about string representation per se any more.
Rust must have null terminated strings at some level, because it has to integrate with platforms where null terminated strings are the API currency for text. Yet, Rust programs don't fall down because of null-terminated strings.
Likewise, if you have a pointer and length, but no language or library support for it, just a "high level assembly language", you will screw it up.
The pointer has to point to something. There will be cases where that something is a fixed buffer on the stack or in a dynamic object. Out of a hundred programmers coding their pointer and length with fixed buffers, a good many will cause overflows, and a good many will write memory-safe code that causes consequences due to silent truncation.
In C, we can cause problems like:
* returning a pointer to a stack-allocated { length, ptr } structure, which turns to garbage.
* leak dynamically-allocated { length, ptr } objects, or use them after freeing.
* making copies of a { length, ptr } structure (e.g. pass by value), and not keeping all of them in sync when ptr and/or length are updated.
* sticking a literal "string" into ptr, but then treating it as dynamic elsewhere and trying to free it
* sticking a literal "string" into ptr, and then somewhere trying to modify a character.