File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -158,6 +158,9 @@ impl<T: 'static> ErasedPtr for FnPtr<T> {
158158 fn is_null ( & self ) -> bool {
159159 FnPtr :: is_null ( self )
160160 }
161+ fn is_dangling ( & self ) -> bool {
162+ false
163+ }
161164}
162165
163166impl < T : ' static > FnPtr < T > {
Original file line number Diff line number Diff line change @@ -1091,6 +1091,7 @@ pub(crate) trait ErasedPtr: std::any::Any {
10911091 fn as_any ( & self ) -> & dyn std:: any:: Any ;
10921092 fn equals ( & self , other : & dyn ErasedPtr ) -> bool ;
10931093 fn is_null ( & self ) -> bool ;
1094+ fn is_dangling ( & self ) -> bool ;
10941095}
10951096
10961097impl PartialEq for dyn ErasedPtr {
@@ -1122,6 +1123,16 @@ where
11221123 fn is_null ( & self ) -> bool {
11231124 Ptr :: is_null ( self )
11241125 }
1126+
1127+ fn is_dangling ( & self ) -> bool {
1128+ match & self . kind {
1129+ PtrKind :: Null => false ,
1130+ PtrKind :: StackSingle ( w) | PtrKind :: HeapSingle ( w) => w. strong_count ( ) == 0 ,
1131+ PtrKind :: Vec ( w) => w. strong_count ( ) == 0 ,
1132+ PtrKind :: StackArray ( w) | PtrKind :: HeapArray ( w) => w. strong_count ( ) == 0 ,
1133+ PtrKind :: Reinterpreted ( data) => data. alloc . is_dangling ( ) ,
1134+ }
1135+ }
11251136}
11261137
11271138#[ derive( Clone ) ]
Original file line number Diff line number Diff line change @@ -104,6 +104,7 @@ pub trait OriginalAlloc {
104104 // Stable address used for pointer equality across PtrKind variants.
105105 fn address ( & self ) -> usize ;
106106 fn delete ( & self ) ;
107+ fn is_dangling ( & self ) -> bool ;
107108}
108109
109110// Read bytes starting at `byte_offset` from a slice of S elements into `buf`.
@@ -178,6 +179,10 @@ impl<T: ByteRepr> OriginalAlloc for SingleOriginalAlloc<T> {
178179 }
179180 assert_eq ! ( Weak :: strong_count( & self . weak) , 0 , "ub: double free" ) ;
180181 }
182+
183+ fn is_dangling ( & self ) -> bool {
184+ self . weak . strong_count ( ) == 0
185+ }
181186}
182187
183188pub ( crate ) trait AsSlice {
@@ -241,4 +246,8 @@ impl<T: AsSlice + 'static> OriginalAlloc for SliceOriginalAlloc<T> {
241246 }
242247 assert_eq ! ( Weak :: strong_count( & self . weak) , 0 , "ub: double free" ) ;
243248 }
249+
250+ fn is_dangling ( & self ) -> bool {
251+ self . weak . strong_count ( ) == 0
252+ }
244253}
You can’t perform that action at this time.
0 commit comments