Skip to content

Commit 4e28065

Browse files
committed
tweak pointer out-of-bounds error message
1 parent bd874a9 commit 4e28065

11 files changed

+38
-27
lines changed

compiler/rustc_middle/src/mir/interpret/error.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ pub enum CheckInAllocMsg {
181181
}
182182

183183
impl fmt::Display for CheckInAllocMsg {
184-
/// When this is printed as an error the context looks like this
185-
/// "{msg}pointer must be in-bounds at offset..."
184+
/// When this is printed as an error the context looks like this:
185+
/// "{msg}0x01 is not a valid pointer".
186186
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
187187
write!(
188188
f,
@@ -318,14 +318,24 @@ impl fmt::Display for UndefinedBehaviorInfo<'_> {
318318
PointerUseAfterFree(a) => {
319319
write!(f, "pointer to {} was dereferenced after this allocation got freed", a)
320320
}
321+
PointerOutOfBounds { alloc_id, offset, size: Size::ZERO, msg, allocation_size } => {
322+
write!(
323+
f,
324+
"{}{} has size {}, so pointer at offset {} is out-of-bounds",
325+
msg,
326+
alloc_id,
327+
allocation_size.bytes(),
328+
offset.bytes(),
329+
)
330+
}
321331
PointerOutOfBounds { alloc_id, offset, size, msg, allocation_size } => write!(
322332
f,
323-
"{}pointer must be in-bounds for {} bytes at offset {}, but {} has size {}",
333+
"{}{} has size {}, so pointer to {} bytes starting at offset {} is out-of-bounds",
324334
msg,
335+
alloc_id,
336+
allocation_size.bytes(),
325337
size.bytes(),
326338
offset.bytes(),
327-
alloc_id,
328-
allocation_size.bytes()
329339
),
330340
DanglingIntPointer(0, CheckInAllocMsg::InboundsTest) => {
331341
write!(f, "null pointer is not a valid pointer for this operation")

src/test/ui/const-ptr/out_of_bounds_read.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0080]: evaluation of constant value failed
44
LL | unsafe { copy_nonoverlapping(src, dst, count) }
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
| |
7-
| memory access failed: pointer must be in-bounds for 4 bytes at offset 4, but alloc7 has size 4
7+
| memory access failed: alloc7 has size 4, so pointer to 4 bytes starting at offset 4 is out-of-bounds
88
| inside `copy_nonoverlapping::<u32>` at $SRC_DIR/core/src/intrinsics.rs:LL:COL
99
|
1010
::: $SRC_DIR/core/src/ptr/mod.rs:LL:COL
@@ -23,7 +23,7 @@ error[E0080]: evaluation of constant value failed
2323
LL | unsafe { copy_nonoverlapping(src, dst, count) }
2424
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2525
| |
26-
| memory access failed: pointer must be in-bounds for 4 bytes at offset 4, but alloc7 has size 4
26+
| memory access failed: alloc7 has size 4, so pointer to 4 bytes starting at offset 4 is out-of-bounds
2727
| inside `copy_nonoverlapping::<u32>` at $SRC_DIR/core/src/intrinsics.rs:LL:COL
2828
|
2929
::: $SRC_DIR/core/src/ptr/mod.rs:LL:COL
@@ -47,7 +47,7 @@ error[E0080]: evaluation of constant value failed
4747
LL | unsafe { copy_nonoverlapping(src, dst, count) }
4848
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4949
| |
50-
| memory access failed: pointer must be in-bounds for 4 bytes at offset 4, but alloc7 has size 4
50+
| memory access failed: alloc7 has size 4, so pointer to 4 bytes starting at offset 4 is out-of-bounds
5151
| inside `copy_nonoverlapping::<u32>` at $SRC_DIR/core/src/intrinsics.rs:LL:COL
5252
|
5353
::: $SRC_DIR/core/src/ptr/mod.rs:LL:COL

src/test/ui/consts/const-eval/ub-nonnull.32bit.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ error[E0080]: evaluation of constant value failed
1313
--> $DIR/ub-nonnull.rs:19:30
1414
|
1515
LL | let out_of_bounds_ptr = &ptr[255];
16-
| ^^^^^^^^ dereferencing pointer failed: pointer must be in-bounds for 256 bytes at offset 0, but alloc11 has size 1
16+
| ^^^^^^^^ dereferencing pointer failed: alloc11 has size 1, so pointer to 256 bytes starting at offset 0 is out-of-bounds
1717

1818
error[E0080]: it is undefined behavior to use this value
1919
--> $DIR/ub-nonnull.rs:23:1

src/test/ui/consts/const-eval/ub-nonnull.64bit.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ error[E0080]: evaluation of constant value failed
1313
--> $DIR/ub-nonnull.rs:19:30
1414
|
1515
LL | let out_of_bounds_ptr = &ptr[255];
16-
| ^^^^^^^^ dereferencing pointer failed: pointer must be in-bounds for 256 bytes at offset 0, but alloc11 has size 1
16+
| ^^^^^^^^ dereferencing pointer failed: alloc11 has size 1, so pointer to 256 bytes starting at offset 0 is out-of-bounds
1717

1818
error[E0080]: it is undefined behavior to use this value
1919
--> $DIR/ub-nonnull.rs:23:1

src/test/ui/consts/const-eval/ub-wide-ptr.32bit.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ error[E0080]: could not evaluate static initializer
302302
--> $DIR/ub-wide-ptr.rs:139:5
303303
|
304304
LL | mem::transmute::<_, &dyn Trait>((&92u8, &3u64))
305-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: pointer must be in-bounds for 12 bytes at offset N, but allocN has size N
305+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: allocN has size N, so pointer to 12 bytes starting at offset N is out-of-bounds
306306

307307
error: aborting due to 28 previous errors
308308

src/test/ui/consts/const-eval/ub-wide-ptr.64bit.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ error[E0080]: could not evaluate static initializer
302302
--> $DIR/ub-wide-ptr.rs:139:5
303303
|
304304
LL | mem::transmute::<_, &dyn Trait>((&92u8, &3u64))
305-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: pointer must be in-bounds for 24 bytes at offset N, but allocN has size N
305+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: allocN has size N, so pointer to 24 bytes starting at offset N is out-of-bounds
306306

307307
error: aborting due to 28 previous errors
308308

src/test/ui/consts/copy-intrinsic.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,21 @@ const COPY_OOB_1: () = unsafe {
2424
let mut x = 0i32;
2525
let dangle = (&mut x as *mut i32).wrapping_add(10);
2626
// Even if the first ptr is an int ptr and this is a ZST copy, we should detect dangling 2nd ptrs.
27-
copy_nonoverlapping(0x100 as *const i32, dangle, 0); //~ evaluation of constant value failed [E0080]
27+
copy_nonoverlapping(0x100 as *const i32, dangle, 0); //~ ERROR evaluation of constant value failed [E0080]
28+
//~| pointer at offset 40 is out-of-bounds
2829
};
2930
const COPY_OOB_2: () = unsafe {
3031
let x = 0i32;
3132
let dangle = (&x as *const i32).wrapping_add(10);
3233
// Even if the second ptr is an int ptr and this is a ZST copy, we should detect dangling 1st ptrs.
33-
copy_nonoverlapping(dangle, 0x100 as *mut i32, 0); //~ evaluation of constant value failed [E0080]
34-
//~| memory access failed: pointer must be in-bounds
34+
copy_nonoverlapping(dangle, 0x100 as *mut i32, 0); //~ ERROR evaluation of constant value failed [E0080]
35+
//~| pointer at offset 40 is out-of-bounds
3536
};
3637

3738
const COPY_SIZE_OVERFLOW: () = unsafe {
3839
let x = 0;
3940
let mut y = 0;
40-
copy(&x, &mut y, 1usize << (mem::size_of::<usize>() * 8 - 1)); //~ evaluation of constant value failed [E0080]
41+
copy(&x, &mut y, 1usize << (mem::size_of::<usize>() * 8 - 1)); //~ ERROR evaluation of constant value failed [E0080]
4142
//~| overflow computing total size of `copy`
4243
};
4344
const COPY_NONOVERLAPPING_SIZE_OVERFLOW: () = unsafe {

src/test/ui/consts/copy-intrinsic.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ error[E0080]: evaluation of constant value failed
22
--> $DIR/copy-intrinsic.rs:27:5
33
|
44
LL | copy_nonoverlapping(0x100 as *const i32, dangle, 0);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: pointer must be in-bounds for 0 bytes at offset 40, but alloc5 has size 4
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: alloc5 has size 4, so pointer at offset 40 is out-of-bounds
66

77
error[E0080]: evaluation of constant value failed
8-
--> $DIR/copy-intrinsic.rs:33:5
8+
--> $DIR/copy-intrinsic.rs:34:5
99
|
1010
LL | copy_nonoverlapping(dangle, 0x100 as *mut i32, 0);
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: pointer must be in-bounds for 0 bytes at offset 40, but alloc7 has size 4
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: alloc7 has size 4, so pointer at offset 40 is out-of-bounds
1212

1313
error[E0080]: evaluation of constant value failed
14-
--> $DIR/copy-intrinsic.rs:40:5
14+
--> $DIR/copy-intrinsic.rs:41:5
1515
|
1616
LL | copy(&x, &mut y, 1usize << (mem::size_of::<usize>() * 8 - 1));
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ overflow computing total size of `copy`
1818

1919
error[E0080]: evaluation of constant value failed
20-
--> $DIR/copy-intrinsic.rs:46:5
20+
--> $DIR/copy-intrinsic.rs:47:5
2121
|
2222
LL | copy_nonoverlapping(&x, &mut y, 1usize << (mem::size_of::<usize>() * 8 - 1));
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ overflow computing total size of `copy_nonoverlapping`

src/test/ui/consts/offset_ub.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ error[E0080]: evaluation of constant value failed
1818
LL | unsafe { intrinsics::offset(self, count) }
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2020
| |
21-
| pointer arithmetic failed: pointer must be in-bounds for 2 bytes at offset 0, but allocN has size 1
21+
| pointer arithmetic failed: allocN has size 1, so pointer to 2 bytes starting at offset 0 is out-of-bounds
2222
| inside `ptr::const_ptr::<impl *const u8>::offset` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
2323
|
2424
::: $DIR/offset_ub.rs:9:43
@@ -32,7 +32,7 @@ error[E0080]: evaluation of constant value failed
3232
LL | unsafe { intrinsics::offset(self, count) }
3333
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3434
| |
35-
| pointer arithmetic failed: pointer must be in-bounds for 101 bytes at offset 0, but allocN has size 100
35+
| pointer arithmetic failed: allocN has size 100, so pointer to 101 bytes starting at offset 0 is out-of-bounds
3636
| inside `ptr::const_ptr::<impl *const u8>::offset` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
3737
|
3838
::: $DIR/offset_ub.rs:10:45
@@ -102,7 +102,7 @@ error[E0080]: evaluation of constant value failed
102102
LL | unsafe { intrinsics::offset(self, count) }
103103
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
104104
| |
105-
| pointer arithmetic failed: pointer must be in-bounds for 1 bytes at offset 0, but allocN has size 0
105+
| pointer arithmetic failed: allocN has size 0, so pointer to 1 bytes starting at offset 0 is out-of-bounds
106106
| inside `ptr::const_ptr::<impl *const u8>::offset` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
107107
|
108108
::: $DIR/offset_ub.rs:17:50

src/test/ui/consts/ptr_comparisons.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const _: *const usize = unsafe { (FOO as *const usize).offset(2) };
6363
const _: *const u8 =
6464
unsafe { std::ptr::addr_of!((*(FOO as *const usize as *const [u8; 1000]))[999]) };
6565
//~^ ERROR evaluation of constant value failed
66-
//~| pointer must be in-bounds
66+
//~| out-of-bounds
6767

6868
const _: usize = unsafe { std::mem::transmute::<*const usize, usize>(FOO) + 4 };
6969
//~^ ERROR any use of this value will cause an error

src/test/ui/consts/ptr_comparisons.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0080]: evaluation of constant value failed
44
LL | unsafe { intrinsics::offset(self, count) }
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
| |
7-
| pointer arithmetic failed: pointer must be in-bounds for $TWO_WORDS bytes at offset 0, but alloc3 has size $WORD
7+
| pointer arithmetic failed: alloc3 has size $WORD, so pointer to $TWO_WORDS bytes starting at offset 0 is out-of-bounds
88
| inside `ptr::const_ptr::<impl *const usize>::offset` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
99
|
1010
::: $DIR/ptr_comparisons.rs:61:34
@@ -16,7 +16,7 @@ error[E0080]: evaluation of constant value failed
1616
--> $DIR/ptr_comparisons.rs:64:33
1717
|
1818
LL | unsafe { std::ptr::addr_of!((*(FOO as *const usize as *const [u8; 1000]))[999]) };
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ dereferencing pointer failed: pointer must be in-bounds for 1000 bytes at offset 0, but alloc3 has size $WORD
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ dereferencing pointer failed: alloc3 has size $WORD, so pointer to 1000 bytes starting at offset 0 is out-of-bounds
2020

2121
error: any use of this value will cause an error
2222
--> $DIR/ptr_comparisons.rs:68:27

0 commit comments

Comments
 (0)