improve scoping of unsafe blocks
This commit is contained in:
+10
-14
@@ -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(),
|
||||
|
||||
Reference in New Issue
Block a user