Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 3d3f325

Browse files
committed
Auto merge of rust-lang#15189 - HKalbasi:mir, r=HKalbasi
Fix overflow checking in shift operator
2 parents 46cd8b8 + 15a0da6 commit 3d3f325

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

crates/hir-ty/src/consteval/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ fn bit_op() {
108108
check_fail(r#"const GOAL: i8 = 1 << 8"#, |e| {
109109
e == ConstEvalError::MirEvalError(MirEvalError::Panic("Overflow in Shl".to_string()))
110110
});
111+
check_number(r#"const GOAL: i32 = 100000000i32 << 11"#, (100000000i32 << 11) as i128);
111112
}
112113

113114
#[test]

crates/hir-ty/src/mir/eval.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,13 +1037,18 @@ impl Evaluator<'_> {
10371037
BinOp::Shr => l128.checked_shr(shift_amount),
10381038
_ => unreachable!(),
10391039
};
1040+
if shift_amount as usize >= lc.len() * 8 {
1041+
return Err(MirEvalError::Panic(format!(
1042+
"Overflow in {op:?}"
1043+
)));
1044+
}
10401045
if let Some(r) = r {
10411046
break 'b r;
10421047
}
10431048
};
10441049
return Err(MirEvalError::Panic(format!("Overflow in {op:?}")));
10451050
};
1046-
check_overflow(r)?
1051+
Owned(r.to_le_bytes()[..lc.len()].to_vec())
10471052
}
10481053
BinOp::Offset => not_supported!("offset binop"),
10491054
}

0 commit comments

Comments
 (0)