fix: Make from handle function unsafe

This commit is contained in:
Mark Giraud
2023-08-14 13:38:13 +02:00
parent 44ca44ea1e
commit 5318fcda33

View File

@@ -54,7 +54,6 @@ pub use crate::{
use alloc::{boxed::Box, rc::Rc, vec::Vec}; use alloc::{boxed::Box, rc::Rc, vec::Vec};
use core::{cell::UnsafeCell, ptr}; use core::{cell::UnsafeCell, ptr};
use bitflags::Flags;
use ffi::uc_handle; use ffi::uc_handle;
use libc::c_void; use libc::c_void;
@@ -166,13 +165,14 @@ impl<'a> Unicorn<'a, ()> {
pub fn new(arch: Arch, mode: Mode) -> Result<Unicorn<'a, ()>, uc_error> { pub fn new(arch: Arch, mode: Mode) -> Result<Unicorn<'a, ()>, uc_error> {
Self::new_with_data(arch, mode, ()) Self::new_with_data(arch, mode, ())
} }
}
impl<'a> TryFrom<uc_handle> for Unicorn<'a, ()> { /// # Safety
type Error = uc_error; /// The function has to be called with a valid uc_handle pointer
/// that was previously allocated by a call to uc_open.
#[allow(clippy::not_unsafe_ptr_arg_deref)] /// Calling the function with a non null pointer value that
fn try_from(handle: uc_handle) -> Result<Unicorn<'a, ()>, uc_error> { /// does not point to a unicorn instance will cause undefined
/// behavior.
pub unsafe fn from_handle(handle: uc_handle) -> Result<Unicorn<'a, ()>, uc_error> {
if handle.is_null() { if handle.is_null() {
return Err(uc_error::HANDLE); return Err(uc_error::HANDLE);
} }