Skip to content

Commit 23190dd

Browse files
committed
swap_typed_nonoverlapping: properly detect overlap even when swapping scalar values
1 parent 2d1f266 commit 23190dd

4 files changed

+25
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: Undefined Behavior: constructing invalid value: encountered 0x03, but expected a boolean
2+
--> tests/fail/intrinsics/typed-swap-invalid-scalar.rs:LL:CC
3+
|
4+
LL | typed_swap_nonoverlapping(a, b);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x03, but expected a boolean
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 `invalid_scalar` at tests/fail/intrinsics/typed-swap-invalid-scalar.rs:LL:CC
11+
note: inside `main`
12+
--> tests/fail/intrinsics/typed-swap-invalid-scalar.rs:LL:CC
13+
|
14+
LL | invalid_scalar();
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+

tests/fail/intrinsics/typed-swap-invalid-scalar.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
//@revisions: left right
12
#![feature(core_intrinsics)]
23
#![feature(rustc_attrs)]
34

45
use std::intrinsics::typed_swap_nonoverlapping;
56
use std::ptr::addr_of_mut;
67

78
fn invalid_scalar() {
8-
let mut a = 1_u8;
9-
let mut b = 2_u8;
9+
// We run the test twice, with either the left or the right side being invalid.
10+
let mut a = if cfg!(left) { 2_u8} else { 1_u8 };
11+
let mut b = if cfg!(right) { 3_u8} else { 1_u8 };
1012
unsafe {
1113
let a = addr_of_mut!(a).cast::<bool>();
1214
let b = addr_of_mut!(b).cast::<bool>();

tests/fail/intrinsics/typed-swap-overlap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::intrinsics::typed_swap_nonoverlapping;
55
use std::ptr::addr_of_mut;
66

77
fn main() {
8-
let mut a = [0_u8; 100];
8+
let mut a = 0_u8;
99
unsafe {
1010
let a = addr_of_mut!(a);
1111
typed_swap_nonoverlapping(a, a); //~ERROR: called on overlapping ranges

0 commit comments

Comments
 (0)