Skip to content

Commit

Permalink
Auto merge of rust-lang#15189 - HKalbasi:mir, r=HKalbasi
Browse files Browse the repository at this point in the history
Fix overflow checking in shift operator
  • Loading branch information
bors committed Jul 1, 2023
2 parents 46cd8b8 + 15a0da6 commit 3d3f325
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/hir-ty/src/consteval/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ fn bit_op() {
check_fail(r#"const GOAL: i8 = 1 << 8"#, |e| {
e == ConstEvalError::MirEvalError(MirEvalError::Panic("Overflow in Shl".to_string()))
});
check_number(r#"const GOAL: i32 = 100000000i32 << 11"#, (100000000i32 << 11) as i128);
}

#[test]
Expand Down
7 changes: 6 additions & 1 deletion crates/hir-ty/src/mir/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1037,13 +1037,18 @@ impl Evaluator<'_> {
BinOp::Shr => l128.checked_shr(shift_amount),
_ => unreachable!(),
};
if shift_amount as usize >= lc.len() * 8 {
return Err(MirEvalError::Panic(format!(
"Overflow in {op:?}"
)));
}
if let Some(r) = r {
break 'b r;
}
};
return Err(MirEvalError::Panic(format!("Overflow in {op:?}")));
};
check_overflow(r)?
Owned(r.to_le_bytes()[..lc.len()].to_vec())
}
BinOp::Offset => not_supported!("offset binop"),
}
Expand Down

0 comments on commit 3d3f325

Please sign in to comment.