From 417b61f271f1883143d3a2ad42c4aba4c8259850 Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Wed, 17 Jul 2024 05:53:20 +0000 Subject: [PATCH] Narrow the scope of the ReadFile unsafe block --- std/src/sys/pal/windows/handle.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/std/src/sys/pal/windows/handle.rs b/std/src/sys/pal/windows/handle.rs index 4553e3a232c9f..aaa1831dcc24d 100644 --- a/std/src/sys/pal/windows/handle.rs +++ b/std/src/sys/pal/windows/handle.rs @@ -143,7 +143,7 @@ impl Handle { ) -> io::Result> { // SAFETY: We have exclusive access to the buffer and it's up to the caller to // ensure the OVERLAPPED pointer is valid for the lifetime of this function. - unsafe { + let (res, amt) = unsafe { let len = cmp::min(buf.len(), u32::MAX as usize) as u32; let mut amt = 0; let res = cvt(c::ReadFile( @@ -153,16 +153,17 @@ impl Handle { &mut amt, overlapped, )); - match res { - Ok(_) => Ok(Some(amt as usize)), - Err(e) => { - if e.raw_os_error() == Some(c::ERROR_IO_PENDING as i32) { - Ok(None) - } else if e.raw_os_error() == Some(c::ERROR_BROKEN_PIPE as i32) { - Ok(Some(0)) - } else { - Err(e) - } + (res, amt) + }; + match res { + Ok(_) => Ok(Some(amt as usize)), + Err(e) => { + if e.raw_os_error() == Some(c::ERROR_IO_PENDING as i32) { + Ok(None) + } else if e.raw_os_error() == Some(c::ERROR_BROKEN_PIPE as i32) { + Ok(Some(0)) + } else { + Err(e) } } }