Skip to content

Commit d3a1428

Browse files
committed
Replace Ptr::delete_array with Ptr::delete
There's no need to catch all those UB cases; we can refine into working code.
1 parent 77c219a commit d3a1428

27 files changed

Lines changed: 42 additions & 75 deletions

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,7 +1878,7 @@ bool ConverterRefCount::VisitCXXNewExpr(clang::CXXNewExpr *expr) {
18781878
bool ConverterRefCount::VisitCXXDeleteExpr(clang::CXXDeleteExpr *expr) {
18791879
if (!TypeNeedsDestruction(expr->getDestroyedType())) {
18801880
Convert(expr->getArgument());
1881-
StrCat(expr->isArrayForm() ? ".delete_array()" : ".delete()");
1881+
StrCat(".delete()");
18821882
return false;
18831883
}
18841884

@@ -1889,10 +1889,10 @@ bool ConverterRefCount::VisitCXXDeleteExpr(clang::CXXDeleteExpr *expr) {
18891889
StrCat(std::format("for __i in 0..__p.len() {{ __p.offset(__i as "
18901890
"isize).{}(); }}",
18911891
kDestructorName));
1892-
StrCat("__p.delete_array()", token::kSemiColon);
1892+
StrCat("__p.delete();");
18931893
} else {
1894-
StrCat(std::format("__p.{}()", kDestructorName), token::kSemiColon);
1895-
StrCat("__p.delete()", token::kSemiColon);
1894+
StrCat(std::format("__p.{}();", kDestructorName));
1895+
StrCat("__p.delete();");
18961896
}
18971897
return false;
18981898
}

‎libcc2rs/src/alloc.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ pub fn malloc_refcount(a0: usize) -> AnyPtr {
99
}
1010

1111
pub fn free_refcount(a0: AnyPtr) {
12-
if a0.is_null() {
13-
return;
12+
if !a0.is_null() {
13+
a0.reinterpret_cast::<u8>().delete();
1414
}
15-
a0.reinterpret_cast::<u8>().delete_array();
1615
}
1716

1817
pub fn realloc_refcount(a0: AnyPtr, a1: usize) -> AnyPtr {
1918
if a0.is_null() {
2019
return malloc_refcount(a1);
2120
}
2221
let __new = Ptr::alloc_array(vec![0u8; a1].into_boxed_slice()).to_any();
23-
let __n = a1.min(a0.reinterpret_cast::<u8>().len());
22+
let __src = a0.reinterpret_cast::<u8>();
23+
let __n = a1.min(__src.len());
2424
__new.memcpy(&a0, __n);
25-
a0.reinterpret_cast::<u8>().delete_array();
25+
__src.delete();
2626
__new
2727
}
2828

‎libcc2rs/src/rc.rs‎

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -220,25 +220,14 @@ impl<T> Ptr<T> {
220220
pub fn delete(&self) {
221221
match &self.kind {
222222
PtrKind::HeapSingle(weak) => {
223-
assert_eq!(self.offset, 0, "ub: invalid delete");
224223
assert_eq!(Weak::strong_count(weak), 1, "ub: invalid delete");
225224
unsafe {
226225
let strong = weak.upgrade().expect("ub: dangling pointer");
227226
Rc::from_raw(Rc::as_ptr(&strong));
228227
}
229228
assert_eq!(Weak::strong_count(weak), 0, "ub: double free");
230229
}
231-
PtrKind::Reinterpreted(data) => data.alloc.delete(),
232-
PtrKind::Null => {}
233-
_ => panic!("ub: invalid delete"),
234-
}
235-
}
236-
237-
#[inline]
238-
pub fn delete_array(&self) {
239-
match &self.kind {
240230
PtrKind::HeapArray(weak) => {
241-
assert_eq!(self.offset, 0, "ub: invalid delete");
242231
assert_eq!(Weak::strong_count(weak), 1, "ub: invalid delete");
243232
unsafe {
244233
let strong = weak.upgrade().expect("ub: dangling pointer");
@@ -247,7 +236,6 @@ impl<T> Ptr<T> {
247236
assert_eq!(Weak::strong_count(weak), 0, "ub: double free");
248237
}
249238
PtrKind::HeapVec(weak) => {
250-
assert_eq!(self.offset, 0, "ub: invalid delete");
251239
assert_eq!(Weak::strong_count(weak), 1, "ub: invalid delete");
252240
unsafe {
253241
let strong = weak.upgrade().expect("ub: dangling pointer");
@@ -1025,31 +1013,31 @@ mod tests {
10251013
let p: Ptr<Vec<i32>> = Ptr::alloc(vec![1, 2, 3]);
10261014
let q = p.decay();
10271015
assert_eq!(q.offset(2).read(), 3);
1028-
q.delete_array();
1016+
q.delete();
10291017
}
10301018

10311019
#[test]
10321020
fn decay_heap_array_can_be_freed() {
10331021
let p: Ptr<Box<[i32]>> = Ptr::alloc(vec![1, 2, 3].into_boxed_slice());
10341022
let q = p.decay();
10351023
assert_eq!(q.offset(2).read(), 3);
1036-
q.delete_array();
1024+
q.delete();
10371025
}
10381026

10391027
#[test]
10401028
#[should_panic(expected = "ub: invalid delete")]
10411029
fn decay_stack_vec_cannot_be_freed() {
10421030
let v: Value<Vec<i32>> = Rc::new(RefCell::new(vec![1, 2, 3]));
10431031
let p: Ptr<Vec<i32>> = v.as_pointer();
1044-
p.decay().delete_array();
1032+
p.decay().delete();
10451033
}
10461034

10471035
#[test]
10481036
#[should_panic(expected = "ub: invalid delete")]
10491037
fn decay_stack_array_cannot_be_freed() {
10501038
let v: Value<Box<[i32]>> = Rc::new(RefCell::new(vec![1, 2, 3].into_boxed_slice()));
10511039
let p: Ptr<Box<[i32]>> = (&v as &dyn AsPointer<Box<[i32]>>).as_pointer();
1052-
p.decay().delete_array();
1040+
p.decay().delete();
10531041
}
10541042

10551043
#[test]

‎rules/ifaddrs/tgt_refcount.rs‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,9 @@ fn f2(a0: Ptr<Ifaddrs>) {
3232
let mut __cur = a0;
3333
while !__cur.is_null() {
3434
let __next = __cur.with(|__i| {
35-
let __name = __i.ifa_name.borrow();
36-
if !__name.is_null() {
37-
__name.delete_array();
38-
}
39-
let __addr = __i.ifa_addr.borrow();
40-
if !__addr.is_null() {
41-
__addr.delete();
42-
}
43-
let __mask = __i.ifa_netmask.borrow();
44-
if !__mask.is_null() {
45-
__mask.delete();
46-
}
35+
__i.ifa_name.borrow().delete();
36+
__i.ifa_addr.borrow().delete();
37+
__i.ifa_netmask.borrow().delete();
4738
(*__i.ifa_next.borrow()).clone()
4839
});
4940
__cur.delete();

‎tests/ub/out/refcount/ub10.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn main_0() -> i32 {
1818
)));
1919
let ptr: Value<Ptr<i32>> = Rc::new(RefCell::new((*arr.borrow()).offset((1) as isize)));
2020
let out: Value<i32> = Rc::new(RefCell::new(((*ptr.borrow()).read())));
21-
(*ptr.borrow()).delete_array();
21+
(*ptr.borrow()).delete();
2222
return (*out.borrow());
2323
}
2424
pub fn __cpp2rust_init_globals() {}

‎tests/ub/out/refcount/ub14.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn main_0() -> i32 {
1717
.collect::<Box<[i32]>>(),
1818
)));
1919
(*arr1.borrow()).offset((100) as isize).write(1);
20-
(*arr1.borrow()).delete_array();
20+
(*arr1.borrow()).delete();
2121
return 0;
2222
}
2323
pub fn __cpp2rust_init_globals() {}

‎tests/ub/out/refcount/ub15.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn main_0() -> i32 {
1818
)));
1919
let ptr: Value<Ptr<i32>> = Rc::new(RefCell::new((*arr.borrow()).offset((15) as isize)));
2020
let out: Value<i32> = Rc::new(RefCell::new(((*ptr.borrow()).read())));
21-
(*arr.borrow()).delete_array();
21+
(*arr.borrow()).delete();
2222
return (*out.borrow());
2323
}
2424
pub fn __cpp2rust_init_globals() {}

‎tests/ub/out/refcount/ub16.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn main_0() -> i32 {
2525
.offset((4) as isize)
2626
.read()),
2727
));
28-
(*p1.borrow()).delete_array();
28+
(*p1.borrow()).delete();
2929
return 0;
3030
}
3131
pub fn __cpp2rust_init_globals() {}

‎tests/ub/out/refcount/ub18.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn main() {
1313
fn main_0() -> i32 {
1414
let arr: Value<Box<[i32]>> = Rc::new(RefCell::new(Box::new([1, 2, 3])));
1515
let p: Value<Ptr<i32>> = Rc::new(RefCell::new((arr.as_pointer() as Ptr<i32>)));
16-
(*p.borrow()).delete_array();
16+
(*p.borrow()).delete();
1717
return 0;
1818
}
1919
pub fn __cpp2rust_init_globals() {}

‎tests/ub/out/refcount/ub19.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
99
pub fn foo_0(array: Ptr<i32>) {
1010
let array: Value<Ptr<i32>> = Rc::new(RefCell::new(array));
11-
(*array.borrow()).delete_array();
11+
(*array.borrow()).delete();
1212
}
1313
pub fn main() {
1414
__cpp2rust_init_globals();

0 commit comments

Comments
 (0)