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

Prevent ICE in const-prop array oob check #55792

Merged
merged 3 commits into from
Nov 11, 2018
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
16 changes: 8 additions & 8 deletions src/librustc_mir/transform/const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,8 @@ impl<'b, 'a, 'tcx> Visitor<'tcx> for ConstPropagator<'b, 'a, 'tcx> {
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 {
Expand Down Expand Up @@ -628,20 +628,20 @@ impl<'b, 'a, 'tcx> Visitor<'tcx> for ConstPropagator<'b, 'a, 'tcx> {
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: \
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/consts/const-prop-ice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
[0; 3][3u64 as usize]; //~ ERROR the len is 3 but the index is 3
}
10 changes: 10 additions & 0 deletions src/test/ui/consts/const-prop-ice.stderr
Original file line number Diff line number Diff line change
@@ -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

5 changes: 5 additions & 0 deletions src/test/ui/consts/const-prop-ice2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
enum Enum { One=1 }
let xs=[0;1 as usize];
println!("{}", xs[Enum::One as usize]); //~ ERROR the len is 1 but the index is 1
}
10 changes: 10 additions & 0 deletions src/test/ui/consts/const-prop-ice2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: index out of bounds: the len is 1 but the index is 1
--> $DIR/const-prop-ice2.rs:4:20
|
LL | println!("{}", xs[Enum::One as usize]); //~ ERROR the len is 1 but the index is 1
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: #[deny(const_err)] on by default

error: aborting due to previous error