Skip to content

Commit 5ceee2b

Browse files
committed
Auto merge of #3150 - RalfJung:undercore-match, r=RalfJung
make sure we catch UB in match place even with _ pattern Fixes #2360
2 parents 7577b35 + 28222ce commit 5ceee2b

7 files changed

+85
-8
lines changed

tests/fail/dangling_pointers/dangling_pointer_project_underscore.stderr tests/fail/dangling_pointers/dangling_pointer_project_underscore_let.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
error: Undefined Behavior: out-of-bounds pointer arithmetic: ALLOC has been freed, so this pointer is dangling
2-
--> $DIR/dangling_pointer_project_underscore.rs:LL:CC
2+
--> $DIR/dangling_pointer_project_underscore_let.rs:LL:CC
33
|
44
LL | let _ = (*p).1;
55
| ^^^^^^ out-of-bounds pointer arithmetic: ALLOC has been freed, so this pointer is dangling
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
99
help: ALLOC was allocated here:
10-
--> $DIR/dangling_pointer_project_underscore.rs:LL:CC
10+
--> $DIR/dangling_pointer_project_underscore_let.rs:LL:CC
1111
|
1212
LL | let b = Box::new(42);
1313
| ^^^^^^^^^^^^
1414
help: ALLOC was deallocated here:
15-
--> $DIR/dangling_pointer_project_underscore.rs:LL:CC
15+
--> $DIR/dangling_pointer_project_underscore_let.rs:LL:CC
1616
|
1717
LL | };
1818
| ^
1919
= note: BACKTRACE (of the first span):
20-
= note: inside `main` at $DIR/dangling_pointer_project_underscore.rs:LL:CC
20+
= note: inside `main` at $DIR/dangling_pointer_project_underscore_let.rs:LL:CC
2121

2222
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
2323

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Make sure we find these even with many checks disabled.
2+
//@compile-flags: -Zmiri-disable-alignment-check -Zmiri-disable-stacked-borrows -Zmiri-disable-validation
3+
4+
fn main() {
5+
let p = {
6+
let b = Box::new(42);
7+
&*b as *const i32 as *const (u8, u8, u8, u8)
8+
};
9+
unsafe {
10+
let _: u8 = (*p).1; //~ ERROR: out-of-bounds pointer arithmetic
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error: Undefined Behavior: out-of-bounds pointer arithmetic: ALLOC has been freed, so this pointer is dangling
2+
--> $DIR/dangling_pointer_project_underscore_let_type_annotation.rs:LL:CC
3+
|
4+
LL | let _: u8 = (*p).1;
5+
| ^^^^^^ out-of-bounds pointer arithmetic: ALLOC has been freed, so this pointer is dangling
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+
help: ALLOC was allocated here:
10+
--> $DIR/dangling_pointer_project_underscore_let_type_annotation.rs:LL:CC
11+
|
12+
LL | let b = Box::new(42);
13+
| ^^^^^^^^^^^^
14+
help: ALLOC was deallocated here:
15+
--> $DIR/dangling_pointer_project_underscore_let_type_annotation.rs:LL:CC
16+
|
17+
LL | };
18+
| ^
19+
= note: BACKTRACE (of the first span):
20+
= note: inside `main` at $DIR/dangling_pointer_project_underscore_let_type_annotation.rs:LL:CC
21+
22+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
23+
24+
error: aborting due to previous error
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Make sure we find these even with many checks disabled.
2+
//@compile-flags: -Zmiri-disable-alignment-check -Zmiri-disable-stacked-borrows -Zmiri-disable-validation
3+
4+
fn main() {
5+
let p = {
6+
let b = Box::new(42);
7+
&*b as *const i32 as *const (u8, u8, u8, u8)
8+
};
9+
unsafe {
10+
match (*p).1 {
11+
//~^ ERROR: out-of-bounds pointer arithmetic
12+
_ => {}
13+
}
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error: Undefined Behavior: out-of-bounds pointer arithmetic: ALLOC has been freed, so this pointer is dangling
2+
--> $DIR/dangling_pointer_project_underscore_match.rs:LL:CC
3+
|
4+
LL | match (*p).1 {
5+
| ^^^^^^ out-of-bounds pointer arithmetic: ALLOC has been freed, so this pointer is dangling
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+
help: ALLOC was allocated here:
10+
--> $DIR/dangling_pointer_project_underscore_match.rs:LL:CC
11+
|
12+
LL | let b = Box::new(42);
13+
| ^^^^^^^^^^^^
14+
help: ALLOC was deallocated here:
15+
--> $DIR/dangling_pointer_project_underscore_match.rs:LL:CC
16+
|
17+
LL | };
18+
| ^
19+
= note: BACKTRACE (of the first span):
20+
= note: inside `main` at $DIR/dangling_pointer_project_underscore_match.rs:LL:CC
21+
22+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
23+
24+
error: aborting due to previous error
25+

tests/pass/underscore_pattern.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
use std::ptr;
44

55
fn main() {
6-
dangling_deref_match();
7-
union_uninhabited_match();
6+
dangling_match();
7+
invalid_match();
88
dangling_let();
99
invalid_let();
1010
dangling_let_type_annotation();
1111
invalid_let_type_annotation();
1212
}
1313

14-
fn dangling_deref_match() {
14+
fn dangling_match() {
1515
let p = {
1616
let b = Box::new(42);
1717
&*b as *const i32
@@ -23,7 +23,7 @@ fn dangling_deref_match() {
2323
}
2424
}
2525

26-
fn union_uninhabited_match() {
26+
fn invalid_match() {
2727
#[derive(Copy, Clone)]
2828
enum Void {}
2929
union Uninit<T: Copy> {

0 commit comments

Comments
 (0)