improve scoping of unsafe blocks

This commit is contained in:
2025-12-03 11:50:15 +00:00
parent 2138464fec
commit bd51e3e2e2
3 changed files with 55 additions and 69 deletions
+10 -14
View File
@@ -21,25 +21,21 @@ impl Deref for Handle {
impl Drop for Handle {
fn drop(&mut self) {
unsafe {
if let Err(why) = CloseHandle(**self) {
log::error!("failed to close handle: {why}");
};
}
if let Err(why) = unsafe { CloseHandle(**self) } {
log::error!("failed to close handle: {why}");
};
}
}
impl Handle {
pub fn from_pid(pid: u32) -> Result<Self, crate::Error> {
let handle = unsafe {
OpenProcess(PROCESS_ALL_ACCESS, false, pid)
.or_else(|_| OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE, false, pid))
.map_err(|why| {
crate::Error::ObtainHandleError(format!(
"failed to open process with needed access: {why}",
))
})?
};
let handle = unsafe { OpenProcess(PROCESS_ALL_ACCESS, false, pid) }
.or_else(|_| unsafe { OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE, false, pid) })
.map_err(|why| {
crate::Error::ObtainHandleError(format!(
"failed to open process with needed access: {why}",
))
})?;
if handle == INVALID_HANDLE_VALUE {
return Err(crate::Error::ObtainHandleError(
"failed to get a valid handle".to_string(),