Skip to content

Commit 7e308eb

Browse files
authored
Fix null initialization (#252)
1 parent 5907a12 commit 7e308eb

4 files changed

Lines changed: 55 additions & 0 deletions

File tree

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,7 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
13151315
return false;
13161316
}
13171317
if (expr->getCastKind() == clang::CK_NullToPointer) {
1318+
PushConversionKind push(*this, ConversionKind::Unboxed);
13181319
StrCat(GetDefaultAsString(expr->getType()));
13191320
computed_expr_type_ = ComputedExprType::FreshPointer;
13201321
return false;

tests/unit/out/refcount/ptr_cast_init.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,30 @@ fn main_0() -> i32 {
7474
let hp: Value<Ptr<header>> = Rc::new(RefCell::new((h.as_pointer())));
7575
let v: Value<Ptr<view>> = Rc::new(RefCell::new((*hp.borrow()).reinterpret_cast::<view>()));
7676
assert!(((((*(*(*v.borrow()).upgrade().deref()).tag.borrow()) == 7) as i32) != 0));
77+
let data: Value<Box<[u8]>> = Rc::new(RefCell::new(Box::from(*b"hi\0")));
78+
let vp: Value<AnyPtr> = Rc::new(RefCell::new(
79+
((data.as_pointer() as Ptr<u8>) as Ptr<u8>).to_any(),
80+
));
81+
let n: Value<i32> = Rc::new(RefCell::new(2));
82+
let sel: Value<Ptr<u8>> = Rc::new(RefCell::new(
83+
if ((((*n.borrow()) < 100) as i32) != 0) {
84+
(*vp.borrow()).clone()
85+
} else {
86+
(AnyPtr::default())
87+
}
88+
.reinterpret_cast::<u8>(),
89+
));
90+
assert!((((!((*sel.borrow()).is_null())) as i32) != 0));
91+
assert!(
92+
((((((*sel.borrow()).offset((0) as isize).read()) as i32) == ('h' as i32)) as i32) != 0)
93+
);
94+
(*n.borrow_mut()) = 200;
95+
(*sel.borrow_mut()) = if ((((*n.borrow()) < 100) as i32) != 0) {
96+
(*vp.borrow()).clone()
97+
} else {
98+
(AnyPtr::default())
99+
}
100+
.reinterpret_cast::<u8>();
101+
assert!(((((*sel.borrow()).is_null()) as i32) != 0));
77102
return 0;
78103
}

tests/unit/out/unsafe/ptr_cast_init.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,23 @@ unsafe fn main_0() -> i32 {
3232
let mut hp: *mut header = (&mut h as *mut header);
3333
let mut v: *mut view = (hp as *mut view);
3434
assert!((((((*v).tag) == (7)) as i32) != 0));
35+
let mut data: [libc::c_char; 3] = std::mem::transmute(*b"hi\0");
36+
let mut vp: *mut ::libc::c_void =
37+
(data.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void);
38+
let mut n: i32 = 2;
39+
let mut sel: *mut libc::c_char = (if ((((n) < (100)) as i32) != 0) {
40+
vp
41+
} else {
42+
(0 as *mut ::libc::c_void)
43+
} as *mut libc::c_char);
44+
assert!((((!((sel).is_null())) as i32) != 0));
45+
assert!((((((*sel.offset((0) as isize)) as i32) == ('h' as i32)) as i32) != 0));
46+
n = 200;
47+
sel = (if ((((n) < (100)) as i32) != 0) {
48+
vp
49+
} else {
50+
(0 as *mut ::libc::c_void)
51+
} as *mut libc::c_char);
52+
assert!(((((sel).is_null()) as i32) != 0));
3553
return 0;
3654
}

tests/unit/ptr_cast_init.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <assert.h>
2+
#include <stddef.h>
23

34
struct header {
45
int tag;
@@ -20,5 +21,15 @@ int main(void) {
2021
struct header *hp = &h;
2122
struct view *v = (struct view *)hp;
2223
assert(v->tag == 7);
24+
25+
char data[] = "hi";
26+
void *vp = data;
27+
int n = 2;
28+
char *sel = (n < 100) ? vp : NULL;
29+
assert(sel != 0);
30+
assert(sel[0] == 'h');
31+
n = 200;
32+
sel = (n < 100) ? vp : NULL;
33+
assert(sel == 0);
2334
return 0;
2435
}

0 commit comments

Comments
 (0)