about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLuv-Ray <zhuoxun.yang777@outlook.com>2024-06-01 09:40:46 +0800
committerLuv-Ray <zhuoxun.yang777@outlook.com>2024-06-01 09:40:46 +0800
commitd3c8e6788cfbcc64ab7710cab7a56e276e7d5a7a (patch)
treecc1ddc4814630540ea67b9d84538d1685a9ad40b
parent99cb42c29641aee2cce6521e07960d4de93205c8 (diff)
downloadrust-d3c8e6788cfbcc64ab7710cab7a56e276e7d5a7a.tar.gz
rust-d3c8e6788cfbcc64ab7710cab7a56e276e7d5a7a.zip
check index `value <= 0xFFFF_FF00`
-rw-r--r--compiler/rustc_mir_transform/src/known_panics_lint.rs8
-rw-r--r--tests/crashes/121126.rs4
-rw-r--r--tests/ui/indexing/index-bounds.rs10
-rw-r--r--tests/ui/indexing/index-bounds.stderr16
4 files changed, 32 insertions, 6 deletions
diff --git a/compiler/rustc_mir_transform/src/known_panics_lint.rs b/compiler/rustc_mir_transform/src/known_panics_lint.rs
index 9ba22870403..8b46658b322 100644
--- a/compiler/rustc_mir_transform/src/known_panics_lint.rs
+++ b/compiler/rustc_mir_transform/src/known_panics_lint.rs
@@ -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 },
diff --git a/tests/crashes/121126.rs b/tests/crashes/121126.rs
deleted file mode 100644
index 2ebe91f02de..00000000000
--- a/tests/crashes/121126.rs
+++ /dev/null
@@ -1,4 +0,0 @@
-//@ known-bug: #121126
-fn main() {
-    let _n = 1i64 >> [64][4_294_967_295];
-}
diff --git a/tests/ui/indexing/index-bounds.rs b/tests/ui/indexing/index-bounds.rs
new file mode 100644
index 00000000000..2b318beeeaa
--- /dev/null
+++ b/tests/ui/indexing/index-bounds.rs
@@ -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]
+}
diff --git a/tests/ui/indexing/index-bounds.stderr b/tests/ui/indexing/index-bounds.stderr
new file mode 100644
index 00000000000..51d8c7567a4
--- /dev/null
+++ b/tests/ui/indexing/index-bounds.stderr
@@ -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
+