Skip to content

Commit

Permalink
Narrow the scope of the ReadFile unsafe block
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisDenton committed Jul 17, 2024
1 parent a33abbb commit 2043de1
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions library/std/src/sys/pal/windows/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl Handle {
) -> io::Result<Option<usize>> {
// 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(
Expand All @@ -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)
}
}
}
Expand Down

0 comments on commit 2043de1

Please sign in to comment.