Skip to content

Commit 35e8a8b

Browse files
committed
Actually check result from DuplicateHandle
1 parent 9299678 commit 35e8a8b

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/windows/com.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ impl COMPort {
119119
pub fn try_clone_native(&self) -> Result<COMPort> {
120120
let process_handle: HANDLE = unsafe { GetCurrentProcess() };
121121
let mut cloned_handle: HANDLE = INVALID_HANDLE_VALUE;
122-
unsafe {
122+
123+
let duplicated = unsafe {
123124
DuplicateHandle(
124125
process_handle,
125126
self.raw_handle(),
@@ -128,16 +129,16 @@ impl COMPort {
128129
0,
129130
TRUE,
130131
DUPLICATE_SAME_ACCESS,
131-
);
132-
if cloned_handle != INVALID_HANDLE_VALUE {
133-
Ok(COMPort {
134-
handle: OwnedHandle::from_raw_handle(cloned_handle),
135-
port_name: self.port_name.clone(),
136-
timeout: self.timeout,
137-
})
138-
} else {
139-
Err(super::error::last_os_error())
140-
}
132+
)
133+
};
134+
if duplicated == TRUE && cloned_handle != INVALID_HANDLE_VALUE {
135+
Ok(COMPort {
136+
handle: unsafe { OwnedHandle::from_raw_handle(cloned_handle) },
137+
port_name: self.port_name.clone(),
138+
timeout: self.timeout,
139+
})
140+
} else {
141+
Err(super::error::last_os_error())
141142
}
142143
}
143144

0 commit comments

Comments
 (0)