-
Notifications
You must be signed in to change notification settings - Fork 36
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
Don't lie about buffer initialization #47
Conversation
src/lib.rs
Outdated
// SAFETY: read_uninit does not de-initialize the buffer and guarantees that the first nread | ||
// bytes are initialized. | ||
self.with_context(ctx, |s| unsafe { | ||
match cvt(s.read_uninit(buf.unfilled_mut()))? { | ||
Poll::Ready(nread) => { | ||
unsafe { | ||
buf.assume_init(nread); | ||
} | ||
buf.assume_init(nread); | ||
buf.advance(nread); | ||
Poll::Ready(Ok(())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally you should avoid so large unsafe blocks.
// SAFETY: The `read_uninit` method promises to never deinitialize any of the bytes.
match cvt(s.read_uninit(unsafe { buf.unfilled_mut() }))? {
Poll::Ready(nread) => {
// SAFETY: The `read_uninit` method promises that the first `nread` bytes of the buffer are now initialized.
unsafe { buf.assume_init(nread) };
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated! Sorry for the delay.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happy with this. Feel free to take my nit below or not.
src/lib.rs
Outdated
unsafe { | ||
buf.assume_init(nread); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: If you move the semicolon outside the unsafe block, then rustfmt formats this on one line.
It's formally UB to create a
&mut [u8]
out of possibly-uninitialized bytes. Instead, use the newopenssl
APIs that work directly with&mut [MaybeUninit<u8>]
.Closes #46