@@ -33,32 +33,34 @@ use std::sync::RwLock;
33
33
pub mod c;
34
34
35
35
pub mod error;
36
+ use error:: PointerError ;
36
37
37
38
#[ cfg( all( feature = "std" , feature = "lender" ) ) ]
38
39
lazy_static ! {
39
40
static ref LENT_POINTERS : RwLock <HashSet <usize >> = RwLock :: new( HashSet :: new( ) ) ;
40
41
}
41
42
42
- #[ cfg( all( feature = "std" , feature = "lender" ) ) ]
43
43
#[ 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" ) ) ]
45
56
if let Ok ( lent_pointers) = LENT_POINTERS . read ( ) {
46
57
if !lent_pointers. contains ( & ( pointer as usize ) ) {
47
58
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 ) ;
49
60
}
50
61
} else {
51
62
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 ) ;
62
64
}
63
65
return Ok ( ( ) ) ;
64
66
}
@@ -108,10 +110,10 @@ pub unsafe fn free<T>(pointer: *mut T) {
108
110
#[ doc( alias = "free" ) ]
109
111
#[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
110
112
#[ 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 > {
113
115
#[ cfg( all( feature = "std" , feature = "lender" ) ) ]
114
- invalid_error_check ( pointer) ?;
116
+ validate_pointer ( pointer) ?;
115
117
let boxed = { Box :: from_raw ( pointer) } ;
116
118
#[ cfg( all( feature = "std" , feature = "lender" ) ) ]
117
119
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
128
130
///
129
131
/// Invalid pointer could cause an undefined behavior or heap error and a crash.
130
132
#[ 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) ?;
135
135
return Ok ( & * pointer) ;
136
136
}
137
137
@@ -145,9 +145,7 @@ pub unsafe fn object<'a, T>(pointer: *const T) -> Result<&'a T, crate::error::Po
145
145
///
146
146
/// Invalid pointer could cause an undefined behavior or heap error and a crash.
147
147
#[ 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) ?;
152
150
return Ok ( & mut * pointer) ;
153
151
}
0 commit comments