Skip to content

Commit 5790e3b

Browse files
committed
Add is_dangling
1 parent 2773698 commit 5790e3b

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

‎libcc2rs/src/fn_ptr.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff 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

163166
impl<T: 'static> FnPtr<T> {

‎libcc2rs/src/rc.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff 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

10961097
impl 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)]

‎libcc2rs/src/reinterpret.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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

183188
pub(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
}

0 commit comments

Comments
 (0)