Skip to content

Commit 417b61f

Browse files
committed
Narrow the scope of the ReadFile unsafe block
1 parent b4d1392 commit 417b61f

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

std/src/sys/pal/windows/handle.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl Handle {
143143
) -> io::Result<Option<usize>> {
144144
// SAFETY: We have exclusive access to the buffer and it's up to the caller to
145145
// ensure the OVERLAPPED pointer is valid for the lifetime of this function.
146-
unsafe {
146+
let (res, amt) = unsafe {
147147
let len = cmp::min(buf.len(), u32::MAX as usize) as u32;
148148
let mut amt = 0;
149149
let res = cvt(c::ReadFile(
@@ -153,16 +153,17 @@ impl Handle {
153153
&mut amt,
154154
overlapped,
155155
));
156-
match res {
157-
Ok(_) => Ok(Some(amt as usize)),
158-
Err(e) => {
159-
if e.raw_os_error() == Some(c::ERROR_IO_PENDING as i32) {
160-
Ok(None)
161-
} else if e.raw_os_error() == Some(c::ERROR_BROKEN_PIPE as i32) {
162-
Ok(Some(0))
163-
} else {
164-
Err(e)
165-
}
156+
(res, amt)
157+
};
158+
match res {
159+
Ok(_) => Ok(Some(amt as usize)),
160+
Err(e) => {
161+
if e.raw_os_error() == Some(c::ERROR_IO_PENDING as i32) {
162+
Ok(None)
163+
} else if e.raw_os_error() == Some(c::ERROR_BROKEN_PIPE as i32) {
164+
Ok(Some(0))
165+
} else {
166+
Err(e)
166167
}
167168
}
168169
}

0 commit comments

Comments
 (0)