-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Undefined behavior with Vec::set_len #111
Comments
Hi, and thanks for the report! The safety requirements listed in the documentation are necessary (and sufficient) to guarantee the safety of calling However in our case we do not end the unsafe block there. Instead, we write to this buffer without reading from it, and then, for the final call, sets the length to the initialized portion. The question is then: is it safe to have |
IIUC, what actually happens in these examples is a bit more subtle:
My concern is that even if It goes on to list some ways a value can be invalid, including "An integer (i*/u*), floating point value (f*), or raw pointer obtained from uninitialized memory, or uninitialized memory in a str." and "A reference or Box that is dangling, unaligned, or points to an invalid value." My reasoning here is that in order to perform (2) above we produce a I think it's possible that the unsafe code guidelines will eventually say that this is allowed behavior. I think that to conform to the current stable language rules these functions would need to operate on While I feel pretty confident in my read of the reference here, I should note that miri does not currently complain at the pattern you describe @gyscos. In particular, see the commented out line 20 which will cause miri to barf if uncommented. I'm nearly positive that the current Rust aliasing rules allow the compiler to insert reads like that one without needing a programmer to write it in the source (because EDIT: I filed rust-lang/miri#1762 for clarification on miri not catching this. EDIT2: Ah, miri doesn't attempt to catch this: "In particular, Miri does currently not check that integers/floats are initialized or that references point to valid data." |
Well, that's unfortunate. I suppose if we want to keep the interface from
|
Yeah, these all seem like valid options to me. Your third point reminded me of the ReadBuf RFC, which apparently has a candidate implementation now. |
I started a It removes most of the It does make the
Indeed, though here we want to directly write to the user's container (like |
That branch has now been merged, the potential UB should no longer apply. |
Hello,
There are several instances within zstd-rs where Vec::set_len are used:
zstd-rs/src/block/compressor.rs
Line 66 in cd0450d
zstd-rs/src/block/decompressor.rs
Line 58 in cd0450d
zstd-rs/src/stream/zio/writer.rs
Line 107 in cd0450d
zstd-rs/src/dict.rs
Line 112 in cd0450d
Documentation for set_len safety states:
The second condition is not met in the instances linked above.
It would also be great if there were more documentation about safety invariants - the samples in the documentation above include examples. That would help establish confidence in the set_len calls following the FFI calls.
The text was updated successfully, but these errors were encountered: