Skip to content

Commit acaf0ae

Browse files
committed
Auto merge of #125821 - Luv-Ray:issue#121126, r=fee1-dead
Check index `value <= 0xFFFF_FF00` <!-- If this PR is related to an unstable feature or an otherwise tracked effort, please link to the relevant tracking issue here. If you don't know of a related tracking issue or there are none, feel free to ignore this. This PR will get automatically assigned to a reviewer. In case you would like a specific user to review your work, you can assign it to them by using r​? <reviewer name> --> fixes #121126 check `idx <= FieldIdx::MAX_AS_U32` before calling `FieldIdx::from_u32` to avoid panic.
2 parents 05965ae + d3c8e67 commit acaf0ae

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

compiler/rustc_mir_transform/src/known_panics_lint.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,12 @@ impl<'tcx> Value<'tcx> {
102102
}
103103
(PlaceElem::Index(idx), Value::Aggregate { fields, .. }) => {
104104
let idx = prop.get_const(idx.into())?.immediate()?;
105-
let idx = prop.ecx.read_target_usize(idx).ok()?;
106-
fields.get(FieldIdx::from_u32(idx.try_into().ok()?)).unwrap_or(&Value::Uninit)
105+
let idx = prop.ecx.read_target_usize(idx).ok()?.try_into().ok()?;
106+
if idx <= FieldIdx::MAX_AS_U32 {
107+
fields.get(FieldIdx::from_u32(idx)).unwrap_or(&Value::Uninit)
108+
} else {
109+
return None;
110+
}
107111
}
108112
(
109113
PlaceElem::ConstantIndex { offset, min_length: _, from_end: false },

tests/crashes/121126.rs

-4
This file was deleted.

tests/ui/indexing/index-bounds.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ build-fail
2+
3+
fn main() {
4+
let _n = [64][200];
5+
//~^ ERROR this operation will panic at runtime [unconditional_panic]
6+
7+
// issue #121126, test index value between 0xFFFF_FF00 and u32::MAX
8+
let _n = [64][u32::MAX as usize - 1];
9+
//~^ ERROR this operation will panic at runtime [unconditional_panic]
10+
}

tests/ui/indexing/index-bounds.stderr

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: this operation will panic at runtime
2+
--> $DIR/index-bounds.rs:4:14
3+
|
4+
LL | let _n = [64][200];
5+
| ^^^^^^^^^ index out of bounds: the length is 1 but the index is 200
6+
|
7+
= note: `#[deny(unconditional_panic)]` on by default
8+
9+
error: this operation will panic at runtime
10+
--> $DIR/index-bounds.rs:8:14
11+
|
12+
LL | let _n = [64][u32::MAX as usize - 1];
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ index out of bounds: the length is 1 but the index is 4294967294
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)