Skip to content

Commit 95e1702

Browse files
committed
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.
1 parent c84f39e commit 95e1702

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

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)