@@ -27,6 +27,15 @@ pub(super) fn lent_type_of<T>(pointer: *const T) -> Option<TypeId> {
2727 lent_pointers. get ( & ( pointer as usize ) ) . copied ( )
2828}
2929
30+ fn writable_lent_pointers ( ) -> RwLockWriteGuard < ' static , HashMap < usize , TypeId > > {
31+ let Ok ( lent_pointers) = LENT_POINTERS . write ( ) else {
32+ log:: error!( "RwLock poisoned, it is not possible to add or remove pointers" ) ;
33+ unreachable ! ( "RwLock poisoned, it is not possible to add or remove pointers" ) ;
34+ } ;
35+
36+ lent_pointers
37+ }
38+
3039/// Use only when lend memory as a [`raw`](crate::raw) pointer.
3140///
3241/// # Panics
@@ -53,15 +62,18 @@ pub(super) fn lend<T: 'static>(pointer: *const T) -> Result<(), PointerError> {
5362/// If the [`RwLock`] used is poisoned, but it only happens if a panic happens
5463/// while holding it. And it's specially reviewed and in a small module to
5564/// avoid panics while holding it.
56- pub ( super ) fn retrieve < T > ( pointer : * const T ) {
57- writable_lent_pointers ( ) . remove ( & ( pointer as usize ) ) ;
58- }
59-
60- fn writable_lent_pointers ( ) -> RwLockWriteGuard < ' static , HashMap < usize , TypeId > > {
61- let Ok ( lent_pointers) = LENT_POINTERS . write ( ) else {
62- log:: error!( "RwLock poisoned, it is not possible to add or remove pointers" ) ;
63- unreachable ! ( ) ;
64- } ;
65-
66- lent_pointers
65+ pub ( super ) fn retrieve < T : ' static > ( pointer : * const T ) -> Result < ( ) , PointerError > {
66+ match writable_lent_pointers ( ) . remove ( & ( pointer as usize ) ) {
67+ Some ( type_id) if type_id != TypeId :: of :: < T > ( ) => {
68+ log:: error!(
69+ "Using a pointer with a different type as an opaque pointer to Rust's data"
70+ ) ;
71+ Err ( PointerError :: InvalidType )
72+ }
73+ None => {
74+ log:: error!( "Using an invalid pointer as an opaque pointer to Rust's data" ) ;
75+ Err ( PointerError :: Invalid )
76+ }
77+ _ => Ok ( ( ) ) ,
78+ }
6779}
0 commit comments