Skip to content

Remove last use of mem::uninitialized from std::io::util #62732

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

Merged
merged 1 commit into from
Jul 18, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/libstd/io/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> io::Result<
where R: Read, W: Write
{
let mut buf = unsafe {
#[allow(deprecated)]
let mut buf: [u8; super::DEFAULT_BUF_SIZE] = mem::uninitialized();
reader.initializer().initialize(&mut buf);
buf
// This is still technically undefined behavior due to creating a reference
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a FIXME here as a reminder?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable to me, I'll add that in

// to uninitialized data, but within libstd we can rely on more guarantees
// than if this code were in an external lib
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't just create a reference to uninitialized data (so the comment is not entirely correct). It actually creates (in the final assume_init) uninitialized (integer) values (not behind a reference).


// FIXME: This should probably be changed to an array of `MaybeUninit<u8>`
// once the `mem::MaybeUninit` slice APIs stabilize
let mut buf: mem::MaybeUninit<[u8; super::DEFAULT_BUF_SIZE]> = mem::MaybeUninit::uninit();
reader.initializer().initialize(&mut *buf.as_mut_ptr());
buf.assume_init()
};

let mut written = 0;
Expand Down