Skip to content

Commit b9c4067

Browse files
committed
Auto merge of rust-lang#95158 - sunfishcode:sunfishcode/windows-8, r=joshtriplett
Preserve the Windows `GetLastError` error in `HandleOrInvalid`. In the `TryFrom<HandleOrInvalid> for OwnedHandle` and `TryFrom<HandleOrNull> for OwnedHandle` implemenations, `forget` the owned handle on the error path, to avoid calling `CloseHandle` on an invalid handle. It's harmless, except that it may overwrite the thread's `GetLastError` error. r? `@joshtriplett`
2 parents 2d15732 + 6c407d0 commit b9c4067

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

library/std/src/fs/tests.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,12 @@ fn read_dir_not_found() {
13591359
assert_eq!(res.err().unwrap().kind(), ErrorKind::NotFound);
13601360
}
13611361

1362+
#[test]
1363+
fn file_open_not_found() {
1364+
let res = File::open("/path/that/does/not/exist");
1365+
assert_eq!(res.err().unwrap().kind(), ErrorKind::NotFound);
1366+
}
1367+
13621368
#[test]
13631369
fn create_dir_all_with_junctions() {
13641370
let tmpdir = tmpdir();

library/std/src/os/windows/io/handle.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,15 @@ impl TryFrom<HandleOrNull> for OwnedHandle {
147147
#[inline]
148148
fn try_from(handle_or_null: HandleOrNull) -> Result<Self, ()> {
149149
let owned_handle = handle_or_null.0;
150-
if owned_handle.handle.is_null() { Err(()) } else { Ok(owned_handle) }
150+
if owned_handle.handle.is_null() {
151+
// Don't call `CloseHandle`; it'd be harmless, except that it could
152+
// overwrite the `GetLastError` error.
153+
forget(owned_handle);
154+
155+
Err(())
156+
} else {
157+
Ok(owned_handle)
158+
}
151159
}
152160
}
153161

@@ -197,7 +205,15 @@ impl TryFrom<HandleOrInvalid> for OwnedHandle {
197205
#[inline]
198206
fn try_from(handle_or_invalid: HandleOrInvalid) -> Result<Self, ()> {
199207
let owned_handle = handle_or_invalid.0;
200-
if owned_handle.handle == c::INVALID_HANDLE_VALUE { Err(()) } else { Ok(owned_handle) }
208+
if owned_handle.handle == c::INVALID_HANDLE_VALUE {
209+
// Don't call `CloseHandle`; it'd be harmless, except that it could
210+
// overwrite the `GetLastError` error.
211+
forget(owned_handle);
212+
213+
Err(())
214+
} else {
215+
Ok(owned_handle)
216+
}
201217
}
202218
}
203219

0 commit comments

Comments
 (0)