Skip to content

Commit a72b3b5

Browse files
committed
refactor: Simplify check of errors in pointers with better names
1 parent 0d08862 commit a72b3b5

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

src/lib.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,34 @@ use std::sync::RwLock;
3333
pub mod c;
3434

3535
pub mod error;
36+
use error::PointerError;
3637

3738
#[cfg(all(feature = "std", feature = "lender"))]
3839
lazy_static! {
3940
static ref LENT_POINTERS: RwLock<HashSet<usize>> = RwLock::new(HashSet::new());
4041
}
4142

42-
#[cfg(all(feature = "std", feature = "lender"))]
4343
#[inline]
44-
fn invalid_error_check<T>(pointer: *const T) -> Result<(), crate::error::PointerError> {
44+
fn validate_pointer_is_not_null<T>(pointer: *const T) -> Result<(), PointerError> {
45+
if pointer.is_null() {
46+
log::error!("Using a NULL pointer as an opaque pointer to Rust's data");
47+
return Err(PointerError::Null);
48+
}
49+
return Ok(());
50+
}
51+
52+
#[inline]
53+
fn validate_pointer<T>(pointer: *const T) -> Result<(), PointerError> {
54+
validate_pointer_is_not_null(pointer)?;
55+
#[cfg(all(feature = "std", feature = "lender"))]
4556
if let Ok(lent_pointers) = LENT_POINTERS.read() {
4657
if !lent_pointers.contains(&(pointer as usize)) {
4758
log::error!("Using an invalid pointer as an opaque pointer to Rust's data");
48-
return Err(crate::error::PointerError::Invalid);
59+
return Err(PointerError::Invalid);
4960
}
5061
} else {
5162
log::error!("RwLock poisoned, it is not possible to check pointers");
52-
return Err(crate::error::PointerError::Invalid);
53-
}
54-
return Ok(());
55-
}
56-
57-
#[inline]
58-
fn null_error_check<T>(pointer: *const T) -> Result<(), crate::error::PointerError> {
59-
if pointer.is_null() {
60-
log::error!("Using a NULL pointer as an opaque pointer to Rust's data");
61-
return Err(crate::error::PointerError::Null);
63+
return Err(PointerError::Invalid);
6264
}
6365
return Ok(());
6466
}
@@ -108,10 +110,10 @@ pub unsafe fn free<T>(pointer: *mut T) {
108110
#[doc(alias = "free")]
109111
#[cfg(any(feature = "alloc", feature = "std"))]
110112
#[inline]
111-
pub unsafe fn own_back<T>(pointer: *mut T) -> Result<T, crate::error::PointerError> {
112-
null_error_check(pointer)?;
113+
#[allow(clippy::not_unsafe_ptr_arg_deref)]
114+
pub unsafe fn own_back<T>(pointer: *mut T) -> Result<T, PointerError> {
113115
#[cfg(all(feature = "std", feature = "lender"))]
114-
invalid_error_check(pointer)?;
116+
validate_pointer(pointer)?;
115117
let boxed = { Box::from_raw(pointer) };
116118
#[cfg(all(feature = "std", feature = "lender"))]
117119
LENT_POINTERS.write().unwrap().remove(&(pointer as usize));
@@ -128,10 +130,8 @@ pub unsafe fn own_back<T>(pointer: *mut T) -> Result<T, crate::error::PointerErr
128130
///
129131
/// Invalid pointer could cause an undefined behavior or heap error and a crash.
130132
#[inline]
131-
pub unsafe fn object<'a, T>(pointer: *const T) -> Result<&'a T, crate::error::PointerError> {
132-
null_error_check(pointer)?;
133-
#[cfg(all(feature = "std", feature = "lender"))]
134-
invalid_error_check(pointer)?;
133+
pub unsafe fn object<'a, T>(pointer: *const T) -> Result<&'a T, PointerError> {
134+
validate_pointer_is_not_null(pointer)?;
135135
return Ok(&*pointer);
136136
}
137137

@@ -145,9 +145,7 @@ pub unsafe fn object<'a, T>(pointer: *const T) -> Result<&'a T, crate::error::Po
145145
///
146146
/// Invalid pointer could cause an undefined behavior or heap error and a crash.
147147
#[inline]
148-
pub unsafe fn mut_object<'a, T>(pointer: *mut T) -> Result<&'a mut T, crate::error::PointerError> {
149-
null_error_check(pointer)?;
150-
#[cfg(all(feature = "std", feature = "lender"))]
151-
invalid_error_check(pointer)?;
148+
pub unsafe fn mut_object<'a, T>(pointer: *mut T) -> Result<&'a mut T, PointerError> {
149+
validate_pointer_is_not_null(pointer)?;
152150
return Ok(&mut *pointer);
153151
}

0 commit comments

Comments
 (0)