-
Notifications
You must be signed in to change notification settings - Fork 293
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
Implementation of BufMut
for &mut [u8]
is unsound
#328
Comments
I'm amazed and yet not surprised. Thanks for pointing this out! It does seem like a tragedy that We may end up needing newtype in |
As sean pointed out, it seems that the source of UB is due to |
While we should obviously fix this, I don't think it merits an emergency patch. We probably can wait until 0.6 (in a few months). This doesn't seem worse than the state of 0.4. |
Yes, if What do you think about my approach of creating special slice type? Would just using it and depending on I agree it's probably not emergency situation, maybe it should be documented, though. |
Something like your slice is probably better. I would rather avoid adding a dependency on a third party crate as we are pushing hard for 1.0. |
Hmm, I see the good reason to avoid it because of pushing for I think that this separate crate should be called |
cc @RalfJung |
Good catch!
Strongly disagreed. This looks to me like someone made the mistake of realizing that
|
Fair, do you think the apparent popularity justifies including some standardized types into |
I don't see one instance of this bug as an indication of popularity. ;) An API that both is geared towards in-place initialization (and thus handles references to One could just as well argue that whatever safely turns an In fact, I think just making fn with_uninit_bufmut(buf1: impl BufMut) {
let mut buf2 = [42];
BufMut::bytes_mut(&mut buf2)[0] = buf1.bytes_mut()[0];
// If `buf1` is uninitialized, now so is `buf2`.
} |
I meant popularity of this crate and possibly pattern. But that may still be overstatement. I think the whole thing is stemming from the fact that we didn't have a nice way of working with uninitialized memory from the beginning, so people were using If the primitives for working with uninit memory were in Rust from the beginning, I don't think we would be having this discussion. Good point about |
Given that, is there a proposed fixed API? My counterexample will work for any |
Well, I proposed to cut out |
Ah I see. Yes that API looks good on a cursory glance. |
I would say either something should exist in |
You mean |
Sure. I use |
Imho this bug warrants a quick documentation update if a fix is taking longer. I was about to use the |
The way BufMut uses MaybeUninit can lead to unsoundness. This replaces MaybeUnit with a type owned by bytes so we can ensure the usage patterns are sound. Refs: #328
Fixed by #433 |
Example how safe code can cause UB using
BufMut
:As discussed at rust-lang/rust#66699 casting
&mut [T]
to&mut MaybeUninit<T>
isn't safe. Thusbytes_mut
must return a newtype that prevents overwriting value with invalid values. See how I addressed it in my cratepossibly_uninit
.It'd be the best to share common newtype slice implementation to not fragment the ecosystem. You can expect full support from me when it comes to using my crate as a dependency. That includes making you maintainers, if you're interested.
The text was updated successfully, but these errors were encountered: