Skip to content

Commit 67a3a68

Browse files
committed
Fix null pointer t est
1 parent d1b17a3 commit 67a3a68

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

src/intptrcast.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ impl<'mir, 'tcx> GlobalStateInner {
121121

122122
let global_state = ecx.machine.intptrcast.borrow();
123123

124-
// Special-case NULL, it is always an invalid pointer.
125-
if addr == 0 || global_state.provenance_mode == ProvenanceMode::Strict {
124+
if global_state.provenance_mode == ProvenanceMode::Strict {
126125
Pointer::new(None, Size::from_bytes(addr))
127126
} else if global_state.provenance_mode == ProvenanceMode::Legacy {
128127
let alloc_id = Self::alloc_id_from_addr(ecx, addr);

tests/compile-fail/permissive_provenance_null.rs

Lines changed: 0 additions & 14 deletions
This file was deleted.

tests/run-pass/ptr_int_permissive_provenance.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,22 @@ fn ptr_roundtrip_imperfect() {
4141
assert_eq!(unsafe { *ptr }, 3);
4242
}
4343

44+
/// Ensure that we can roundtrip through a pointer with an address of 0
45+
fn ptr_roundtrip_null() {
46+
let x = &42;
47+
let x_ptr = x as *const i32;
48+
let x_null_ptr = x_ptr.with_addr(0); // addr 0, but still the provenance of x
49+
let null = x_null_ptr.expose_addr();
50+
assert_eq!(null, 0);
51+
52+
let x_null_ptr_copy = ptr::from_exposed_addr::<i32>(null); // just a roundtrip, so has provenance of x (angelically)
53+
let x_ptr_copy = x_null_ptr_copy.with_addr(x_ptr.addr()); // addr of x and provenance of x
54+
assert_eq!(unsafe { *x_ptr_copy }, 42);
55+
}
56+
4457
fn main() {
4558
ptr_roundtrip_out_of_bounds();
4659
ptr_roundtrip_confusion();
4760
ptr_roundtrip_imperfect();
61+
ptr_roundtrip_null();
4862
}

0 commit comments

Comments
 (0)