Skip to content

Commit 13ed0cf

Browse files
committed
do not use mem::uninitialized in std::io
1 parent 33452b0 commit 13ed0cf

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

src/libcore/fmt/float.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ fn float_to_decimal_common_exact<T>(fmt: &mut Formatter<'_>, num: &T,
1212
unsafe {
1313
let mut buf = MaybeUninit::<[u8; 1024]>::uninit(); // enough for f32 and f64
1414
let mut parts = MaybeUninit::<[flt2dec::Part<'_>; 4]>::uninit();
15-
// FIXME(#53491): Technically, this is calling `get_mut` on an uninitialized
16-
// `MaybeUninit` (here and elsewhere in this file). Revisit this once
15+
// FIXME(#53491): This is calling `get_mut` on an uninitialized
16+
// `MaybeUninit` (here and elsewhere in this file). Revisit this once
1717
// we decided whether that is valid or not.
18-
// Using `freeze` is *not enough*; `flt2dec::Part` is an enum!
18+
// We can do this only because we are libstd and coupled to the compiler.
19+
// (FWIW, using `freeze` would not be enough; `flt2dec::Part` is an enum!)
1920
let formatted = flt2dec::to_exact_fixed_str(flt2dec::strategy::grisu::format_exact,
2021
*num, sign, precision,
2122
false, buf.get_mut(), parts.get_mut());

src/libstd/io/util.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use crate::fmt;
44
use crate::io::{self, Read, Initializer, Write, ErrorKind, BufRead, IoSlice, IoSliceMut};
5-
use crate::mem;
5+
use crate::mem::MaybeUninit;
66

77
/// Copies the entire contents of a reader into a writer.
88
///
@@ -43,27 +43,23 @@ use crate::mem;
4343
pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> io::Result<u64>
4444
where R: Read, W: Write
4545
{
46-
let mut buf = unsafe {
47-
// This is still technically undefined behavior due to creating a reference
48-
// to uninitialized data, but within libstd we can rely on more guarantees
49-
// than if this code were in an external lib
50-
51-
// FIXME: This should probably be changed to an array of `MaybeUninit<u8>`
52-
// once the `mem::MaybeUninit` slice APIs stabilize
53-
let mut buf: mem::MaybeUninit<[u8; super::DEFAULT_BUF_SIZE]> = mem::MaybeUninit::uninit();
54-
reader.initializer().initialize(&mut *buf.as_mut_ptr());
55-
buf.assume_init()
56-
};
46+
let mut buf = MaybeUninit::<[u8; super::DEFAULT_BUF_SIZE]>::uninit();
47+
// FIXME(#53491): This is calling `get_mut` and `get_ref` on an uninitialized
48+
// `MaybeUninit`. Revisit this once we decided whether that is valid or not.
49+
// This is still technically undefined behavior due to creating a reference
50+
// to uninitialized data, but within libstd we can rely on more guarantees
51+
// than if this code were in an external lib
52+
unsafe { reader.initializer().initialize(buf.get_mut()); }
5753

5854
let mut written = 0;
5955
loop {
60-
let len = match reader.read(&mut buf) {
56+
let len = match reader.read(unsafe { buf.get_mut() }) {
6157
Ok(0) => return Ok(written),
6258
Ok(len) => len,
6359
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
6460
Err(e) => return Err(e),
6561
};
66-
writer.write_all(&buf[..len])?;
62+
writer.write_all(unsafe { &buf.get_ref()[..len] })?;
6763
written += len as u64;
6864
}
6965
}

src/libstd/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@
272272
#![feature(libc)]
273273
#![feature(link_args)]
274274
#![feature(linkage)]
275+
#![feature(maybe_uninit_ref)]
275276
#![feature(mem_take)]
276277
#![feature(needs_panic_runtime)]
277278
#![feature(never_type)]

0 commit comments

Comments
 (0)