]> git.lizzy.rs Git - rust.git/commitdiff
Prevent ICE in const-prop array oob check
authorOliver Scherer <github35764891676564198441@oli-obk.de>
Thu, 8 Nov 2018 19:15:13 +0000 (20:15 +0100)
committerOliver Scherer <github35764891676564198441@oli-obk.de>
Thu, 8 Nov 2018 19:15:13 +0000 (20:15 +0100)
src/librustc_mir/transform/const_prop.rs
src/test/ui/consts/const-prop-ice.rs [new file with mode: 0644]
src/test/ui/consts/const-prop-ice.stderr [new file with mode: 0644]

index 4f92ba400481bfc2759e4f1fab4ba40cb3efd934..885d70dc4304dee3e3cd02c3118df7555aab58b6 100644 (file)
@@ -591,8 +591,8 @@ fn visit_terminator_kind(
         if let TerminatorKind::Assert { expected, msg, cond, .. } = kind {
             if let Some(value) = self.eval_operand(cond, source_info) {
                 trace!("assertion on {:?} should be {:?}", value, expected);
-                let expected = Immediate::Scalar(Scalar::from_bool(*expected).into());
-                if expected != value.0.to_immediate() {
+                let expected = ScalarMaybeUndef::from(Scalar::from_bool(*expected));
+                if expected != self.ecx.read_scalar(value.0).unwrap() {
                     // poison all places this operand references so that further code
                     // doesn't use the invalid value
                     match cond {
@@ -628,20 +628,20 @@ fn visit_terminator_kind(
                             let len = self
                                 .eval_operand(len, source_info)
                                 .expect("len must be const");
-                            let len = match len.0.to_immediate() {
-                                Immediate::Scalar(ScalarMaybeUndef::Scalar(Scalar::Bits {
+                            let len = match self.ecx.read_scalar(len.0) {
+                                Ok(ScalarMaybeUndef::Scalar(Scalar::Bits {
                                     bits, ..
                                 })) => bits,
-                                _ => bug!("const len not primitive: {:?}", len),
+                                other => bug!("const len not primitive: {:?}", other),
                             };
                             let index = self
                                 .eval_operand(index, source_info)
                                 .expect("index must be const");
-                            let index = match index.0.to_immediate() {
-                                Immediate::Scalar(ScalarMaybeUndef::Scalar(Scalar::Bits {
+                            let index = match self.ecx.read_scalar(index.0) {
+                                Ok(ScalarMaybeUndef::Scalar(Scalar::Bits {
                                     bits, ..
                                 })) => bits,
-                                _ => bug!("const index not primitive: {:?}", index),
+                                other => bug!("const index not primitive: {:?}", other),
                             };
                             format!(
                                 "index out of bounds: \
diff --git a/src/test/ui/consts/const-prop-ice.rs b/src/test/ui/consts/const-prop-ice.rs
new file mode 100644 (file)
index 0000000..17880ad
--- /dev/null
@@ -0,0 +1,3 @@
+fn main() {
+    [0; 3][3u64 as usize]; //~ ERROR the len is 3 but the index is 3
+}
\ No newline at end of file
diff --git a/src/test/ui/consts/const-prop-ice.stderr b/src/test/ui/consts/const-prop-ice.stderr
new file mode 100644 (file)
index 0000000..749ef95
--- /dev/null
@@ -0,0 +1,10 @@
+error: index out of bounds: the len is 3 but the index is 3
+  --> $DIR/const-prop-ice.rs:2:5
+   |
+LL |     [0; 3][3u64 as usize]; //~ ERROR the len is 3 but the index is 3
+   |     ^^^^^^^^^^^^^^^^^^^^^
+   |
+   = note: #[deny(const_err)] on by default
+
+error: aborting due to previous error
+