Skip to content

Commit 5f99349

Browse files
committed
Auto merge of #3750 - RalfJung:dangling-intermediate-ref, r=RalfJung
add test for intermediate reference in '&(*x).0 as *const i32'
2 parents 6f65362 + 9a23878 commit 5f99349

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![feature(raw_ref_op)]
2+
#![feature(strict_provenance)]
3+
use std::ptr;
4+
5+
fn direct_raw(x: *const (i32, i32)) -> *const i32 {
6+
unsafe { &raw const (*x).0 }
7+
}
8+
9+
// Ensure that if a raw pointer is created via an intermediate
10+
// reference, we catch that. (Just in case someone decides to
11+
// desugar this differenly or so.)
12+
fn via_ref(x: *const (i32, i32)) -> *const i32 {
13+
unsafe { &(*x).0 as *const i32 } //~ERROR: dangling pointer
14+
}
15+
16+
fn main() {
17+
let ptr = ptr::without_provenance(0x10);
18+
direct_raw(ptr); // this is fine
19+
via_ref(ptr); // this is not
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: Undefined Behavior: out-of-bounds pointer use: 0x10[noalloc] is a dangling pointer (it has no provenance)
2+
--> $DIR/dangling_pointer_to_raw_pointer.rs:LL:CC
3+
|
4+
LL | unsafe { &(*x).0 as *const i32 }
5+
| ^^^^^^^ out-of-bounds pointer use: 0x10[noalloc] is a dangling pointer (it has no provenance)
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: BACKTRACE:
10+
= note: inside `via_ref` at $DIR/dangling_pointer_to_raw_pointer.rs:LL:CC
11+
note: inside `main`
12+
--> $DIR/dangling_pointer_to_raw_pointer.rs:LL:CC
13+
|
14+
LL | via_ref(ptr); // this is not
15+
| ^^^^^^^^^^^^
16+
17+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
18+
19+
error: aborting due to 1 previous error
20+

0 commit comments

Comments
 (0)