Skip to content

Commit 26398ea

Browse files
authored
Use checked_mul or leading_zeros for shl overflows (#6186)
Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
1 parent 585f4a1 commit 26398ea

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

naga/src/proc/constant_evaluator.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,9 +1831,13 @@ impl<'a> ConstantEvaluator<'a> {
18311831
_ => return Err(ConstantEvaluatorError::InvalidBinaryOpArgs),
18321832
}),
18331833
(Literal::I32(a), Literal::U32(b)) => Literal::I32(match op {
1834-
BinaryOperator::ShiftLeft => a
1835-
.checked_shl(b)
1836-
.ok_or(ConstantEvaluatorError::ShiftedMoreThan32Bits)?,
1834+
BinaryOperator::ShiftLeft => {
1835+
if (if a.is_negative() { !a } else { a }).leading_zeros() <= b {
1836+
return Err(ConstantEvaluatorError::Overflow("<<".to_string()));
1837+
}
1838+
a.checked_shl(b)
1839+
.ok_or(ConstantEvaluatorError::ShiftedMoreThan32Bits)?
1840+
}
18371841
BinaryOperator::ShiftRight => a
18381842
.checked_shr(b)
18391843
.ok_or(ConstantEvaluatorError::ShiftedMoreThan32Bits)?,
@@ -1859,8 +1863,11 @@ impl<'a> ConstantEvaluator<'a> {
18591863
BinaryOperator::ExclusiveOr => a ^ b,
18601864
BinaryOperator::InclusiveOr => a | b,
18611865
BinaryOperator::ShiftLeft => a
1862-
.checked_shl(b)
1863-
.ok_or(ConstantEvaluatorError::ShiftedMoreThan32Bits)?,
1866+
.checked_mul(
1867+
1u32.checked_shl(b)
1868+
.ok_or(ConstantEvaluatorError::ShiftedMoreThan32Bits)?,
1869+
)
1870+
.ok_or(ConstantEvaluatorError::Overflow("<<".to_string()))?,
18641871
BinaryOperator::ShiftRight => a
18651872
.checked_shr(b)
18661873
.ok_or(ConstantEvaluatorError::ShiftedMoreThan32Bits)?,

0 commit comments

Comments
 (0)