Auto merge of #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`
This commit is contained in:
bors 2022-03-22 05:48:49 +00:00
commit b9c4067417
2 changed files with 24 additions and 2 deletions

View file

@ -1359,6 +1359,12 @@ fn read_dir_not_found() {
assert_eq!(res.err().unwrap().kind(), ErrorKind::NotFound);
}
#[test]
fn file_open_not_found() {
let res = File::open("/path/that/does/not/exist");
assert_eq!(res.err().unwrap().kind(), ErrorKind::NotFound);
}
#[test]
fn create_dir_all_with_junctions() {
let tmpdir = tmpdir();

View file

@ -147,7 +147,15 @@ impl TryFrom<HandleOrNull> for OwnedHandle {
#[inline]
fn try_from(handle_or_null: HandleOrNull) -> Result<Self, ()> {
let owned_handle = handle_or_null.0;
if owned_handle.handle.is_null() { Err(()) } else { Ok(owned_handle) }
if owned_handle.handle.is_null() {
// Don't call `CloseHandle`; it'd be harmless, except that it could
// overwrite the `GetLastError` error.
forget(owned_handle);
Err(())
} else {
Ok(owned_handle)
}
}
}
@ -197,7 +205,15 @@ impl TryFrom<HandleOrInvalid> for OwnedHandle {
#[inline]
fn try_from(handle_or_invalid: HandleOrInvalid) -> Result<Self, ()> {
let owned_handle = handle_or_invalid.0;
if owned_handle.handle == c::INVALID_HANDLE_VALUE { Err(()) } else { Ok(owned_handle) }
if owned_handle.handle == c::INVALID_HANDLE_VALUE {
// Don't call `CloseHandle`; it'd be harmless, except that it could
// overwrite the `GetLastError` error.
forget(owned_handle);
Err(())
} else {
Ok(owned_handle)
}
}
}