How to read() from TcpStream and *append* to Vec<u8> – efficiently

There's an RFC to add something that would let you use it with uninitialized memory without doing unsafe acrobatics. You can find it here.

1 Like

I certainly found the comment valuable. But hey, who needs nuance when you can make sweeping statements.

Not sure where you are missing nuance, or where you found a "sweeping statement". My point was exactly that "being discussed" doesn't imply that relying on the behavior is fine even for now (just because it's not explicitly UB), since it can have future consequences.

When we usually talk about UB, we mean that there are no requirements on the behavior of the particular compiled program produced by the particular compiler. However, that is not the case here: the current version of rustc will never give LLVM any annotations to assume that a reference contains a valid value if the program doesn't try to read it. Accordingly, Miri does not check for the validity of values behind references, and does not throw an error if you simply produce a reference to uninitialized memory.

So I'd say that this behavior is currently defined [1], but it is unstable. Depending on the outcome of the debate, a future version of the compiler may declare it undefined and no longer guarantee the expected behavior. A dependency on unstable behavior may be dangerous, but it is not necessarily unmanageable, as long as one keeps track of the changes to the compiler before updating it. References to uninitialized memory are common enough in existing codebases that I strongly doubt that the compiler would break them without a prominent compatibility notice.


  1. well, insofar as any of our language semantics are well-defined ↩ī¸Ž

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.