Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check index value <= 0xFFFF_FF00 #125821

Merged
merged 1 commit into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions compiler/rustc_mir_transform/src/known_panics_lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,12 @@ impl<'tcx> Value<'tcx> {
}
(PlaceElem::Index(idx), Value::Aggregate { fields, .. }) => {
let idx = prop.get_const(idx.into())?.immediate()?;
let idx = prop.ecx.read_target_usize(idx).ok()?;
fields.get(FieldIdx::from_u32(idx.try_into().ok()?)).unwrap_or(&Value::Uninit)
let idx = prop.ecx.read_target_usize(idx).ok()?.try_into().ok()?;
if idx <= FieldIdx::MAX_AS_U32 {
fields.get(FieldIdx::from_u32(idx)).unwrap_or(&Value::Uninit)
} else {
return None;
}
}
(
PlaceElem::ConstantIndex { offset, min_length: _, from_end: false },
Expand Down
4 changes: 0 additions & 4 deletions tests/crashes/121126.rs

This file was deleted.

10 changes: 10 additions & 0 deletions tests/ui/indexing/index-bounds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//@ build-fail

fn main() {
let _n = [64][200];
//~^ ERROR this operation will panic at runtime [unconditional_panic]

// issue #121126, test index value between 0xFFFF_FF00 and u32::MAX
let _n = [64][u32::MAX as usize - 1];
//~^ ERROR this operation will panic at runtime [unconditional_panic]
}
16 changes: 16 additions & 0 deletions tests/ui/indexing/index-bounds.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error: this operation will panic at runtime
--> $DIR/index-bounds.rs:4:14
|
LL | let _n = [64][200];
| ^^^^^^^^^ index out of bounds: the length is 1 but the index is 200
|
= note: `#[deny(unconditional_panic)]` on by default

error: this operation will panic at runtime
--> $DIR/index-bounds.rs:8:14
|
LL | let _n = [64][u32::MAX as usize - 1];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ index out of bounds: the length is 1 but the index is 4294967294

error: aborting due to 2 previous errors

Loading