Skip to content
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

do not use assume_init in std::io #62746

Merged
merged 3 commits into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions src/libcore/fmt/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ fn float_to_decimal_common_exact<T>(fmt: &mut Formatter<'_>, num: &T,
unsafe {
let mut buf = MaybeUninit::<[u8; 1024]>::uninit(); // enough for f32 and f64
let mut parts = MaybeUninit::<[flt2dec::Part<'_>; 4]>::uninit();
// FIXME(#53491): Technically, this is calling `get_mut` on an uninitialized
// `MaybeUninit` (here and elsewhere in this file). Revisit this once
// FIXME(#53491): This is calling `get_mut` on an uninitialized
// `MaybeUninit` (here and elsewhere in this file). Revisit this once
// we decided whether that is valid or not.
// Using `freeze` is *not enough*; `flt2dec::Part` is an enum!
// We can do this only because we are libstd and coupled to the compiler.
// (FWIW, using `freeze` would not be enough; `flt2dec::Part` is an enum!)
let formatted = flt2dec::to_exact_fixed_str(flt2dec::strategy::grisu::format_exact,
*num, sign, precision,
false, buf.get_mut(), parts.get_mut());
Expand Down
24 changes: 10 additions & 14 deletions src/libstd/io/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::fmt;
use crate::io::{self, Read, Initializer, Write, ErrorKind, BufRead, IoSlice, IoSliceMut};
use crate::mem;
use crate::mem::MaybeUninit;

/// Copies the entire contents of a reader into a writer.
///
Expand Down Expand Up @@ -43,27 +43,23 @@ use crate::mem;
pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> io::Result<u64>
where R: Read, W: Write
{
let mut buf = unsafe {
// This is still technically undefined behavior due to creating a reference
// to uninitialized data, but within libstd we can rely on more guarantees
// than if this code were in an external lib

// 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 buf = MaybeUninit::<[u8; super::DEFAULT_BUF_SIZE]>::uninit();
// FIXME(#53491): This is calling `get_mut` and `get_ref` on an uninitialized
// `MaybeUninit`. Revisit this once we decided whether that is valid or not.
// This is still technically undefined behavior due to creating a reference
// to uninitialized data, but within libstd we can rely on more guarantees
// than if this code were in an external lib.
unsafe { reader.initializer().initialize(buf.get_mut()); }

let mut written = 0;
loop {
let len = match reader.read(&mut buf) {
let len = match reader.read(unsafe { buf.get_mut() }) {
Ok(0) => return Ok(written),
Ok(len) => len,
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
Err(e) => return Err(e),
};
writer.write_all(&buf[..len])?;
writer.write_all(unsafe { &buf.get_ref()[..len] })?;
written += len as u64;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
// Don't link to std. We are std.
#![no_std]

//#![warn(deprecated_in_future)] // FIXME: std still has quite a few uses of `mem::uninitialized`
#![warn(deprecated_in_future)]
#![warn(missing_docs)]
#![warn(missing_debug_implementations)]
#![deny(intra_doc_link_resolution_failure)] // rustdoc is run without -D warnings
Expand Down Expand Up @@ -272,6 +272,7 @@
#![feature(libc)]
#![feature(link_args)]
#![feature(linkage)]
#![feature(maybe_uninit_ref)]
#![feature(mem_take)]
#![feature(needs_panic_runtime)]
#![feature(never_type)]
Expand Down
1 change: 1 addition & 0 deletions src/libstd/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ pub use self::condvar::{Condvar, WaitTimeoutResult};
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::mutex::{Mutex, MutexGuard};
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(bootstrap, allow(deprecated_in_future))]
#[allow(deprecated)]
pub use self::once::{Once, OnceState, ONCE_INIT};
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/cloudabi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(deprecated)] // mem::uninitialized
#![allow(deprecated_in_future)] // mem::uninitialized; becomes `deprecated` when nightly is 1.39

use crate::io::ErrorKind;
use crate::mem;
Expand Down