-
Notifications
You must be signed in to change notification settings - Fork 283
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
peek-poke is unsound #3766
Comments
@SimonSapin says "I think the lang team is not decided yet whether &mut T is UB, when T is valid for any bit pattern", though. |
Cc @RalfJung |
Right, so this is UB right now to keep our options open. However, for the specific case of uninhabited See rust-lang/unsafe-code-guidelines#77 for the UCG discussion on this. If you think it is impossible to do what you are doing here when |
Thoughts @Gankra? |
Yeah it's "Documentation UB" in that it's reserved just to be conservative but it's pretty obvious this pattern needs to be doable somehow, so it's only UB insofar as the lang team has failed to design a mechanism that they're happy with for expressing this pattern. Which is to say, this code is fine today. I maintain the docs that say this is UB, and I reviewed the code, so this code was accepted with full awareness of the situation. Ralf is also aware of my stance on this problem from the discussion we've had on the aforementionned docs. Some day, the lang team might either:
Note that I made peek_from only work with On impls: We control all the users, but it's ~impractical to keep all devs who work on firefox aware of this situation, so we don't generally have tight control, in the fullness of time. That said, there's no point in implementing this for |
Ah, that wasn't clear. (Sometimes type inference isn't helping... like when reading a snippet of 3 lines of code without having any context.^^) But then, why doesn't this work? let mut val = MaybeUninit::uninit();
let bytes = <T>::peek_from(bytes, val.as_mut_ptr());
(val.assume_init(), bytes) |
That's just the same code hidden behind a function call :) |
Yes and no. Code in libstd is special and may make assumptions that code outside libstd may not. But also, for both of them, there's actually no reason to wait for pub unsafe fn peek_from_uninit<T: Copy + Peek>(bytes: *const u8) -> (T, *const u8) {
let mut val = MaybeUninitShim { uninit: () };
let bytes = <T>::peek_from(bytes, &mut val as *mut _ as *mut T);
(val.init, bytes)
} Note that this relies on |
The code predates MaybeUninit's stabilization, so yeah we could reasonably update to use it so we can "forget" about the issue and have it solved for us later. Just not a big deal in my mind. |
Method
peek_from_uninit
creates a&mut T
where theT
is uninitialised, this is UB AFAIK.webrender/peek-poke/src/lib.rs
Lines 33 to 39 in efec276
The text was updated successfully, but these errors were encountered: