diff --git a/library/std/src/fs/tests.rs b/library/std/src/fs/tests.rs index 6d67c396c62..3fa731c9529 100644 --- a/library/std/src/fs/tests.rs +++ b/library/std/src/fs/tests.rs @@ -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(); diff --git a/library/std/src/os/windows/io/handle.rs b/library/std/src/os/windows/io/handle.rs index be2ccbd98e9..120af9f99dd 100644 --- a/library/std/src/os/windows/io/handle.rs +++ b/library/std/src/os/windows/io/handle.rs @@ -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) + } } }