From fee457d3af355f24ef321ea7250e968638f403c8 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 24 Jan 2016 21:14:56 -0800 Subject: std: Fix some behavior without stdio handles On all platforms, reading from stdin where the actual stdin isn't present should return 0 bytes as having been read rather than the entire buffer. On Windows, handle the case where we're inheriting stdio handles but one of them isn't present. Currently the behavior is to fail returning an I/O error but instead this commit corrects it to detecting this situation and propagating the non-set handle. Closes #31167 --- src/libstd/io/stdio.rs | 2 +- src/libstd/sys/windows/process.rs | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 79091fd3d6b..cd2d5e52462 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -112,7 +112,7 @@ impl io::Write for Maybe { impl io::Read for Maybe { fn read(&mut self, buf: &mut [u8]) -> io::Result { match *self { - Maybe::Real(ref mut r) => handle_ebadf(r.read(buf), buf.len()), + Maybe::Real(ref mut r) => handle_ebadf(r.read(buf), 0), Maybe::Fake => Ok(0) } } diff --git a/src/libstd/sys/windows/process.rs b/src/libstd/sys/windows/process.rs index e0f8d6f9df9..61f73b00265 100644 --- a/src/libstd/sys/windows/process.rs +++ b/src/libstd/sys/windows/process.rs @@ -351,10 +351,15 @@ fn make_dirp(d: Option<&OsString>) -> (*const u16, Vec) { impl Stdio { fn to_handle(&self, stdio_id: c::DWORD) -> io::Result { match *self { + // If no stdio handle is available, then inherit means that it + // should still be unavailable so propagate the + // INVALID_HANDLE_VALUE. Stdio::Inherit => { - stdio::get(stdio_id).and_then(|io| { - io.handle().duplicate(0, true, c::DUPLICATE_SAME_ACCESS) - }) + match stdio::get(stdio_id) { + Ok(io) => io.handle().duplicate(0, true, + c::DUPLICATE_SAME_ACCESS), + Err(..) => Ok(Handle::new(c::INVALID_HANDLE_VALUE)), + } } Stdio::Raw(handle) => { RawHandle::new(handle).duplicate(0, true, c::DUPLICATE_SAME_ACCESS) -- cgit 1.4.1-3-g733a5