Skip to content

Commit 0743ed6

Browse files
committed
clamp ldexp exponent to i16
1 parent 3ae01a6 commit 0743ed6

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

Diff for: src/shims/foreign_items.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
597597
"_ldexp" | "ldexp" | "scalbn" => {
598598
let x = this.read_scalar(args[0])?.to_f64()?;
599599
let exp = this.read_scalar(args[1])?.to_i32()?;
600-
let res = x.scalbn(exp.try_into().unwrap());
600+
// Saturating cast to i16. Even those are outside the valid exponent range to
601+
// `scalbn` below will to its over/underflow handling.
602+
let exp = if exp > i16::max_value() as i32 {
603+
i16::max_value()
604+
} else if exp < i16::min_value() as i32 {
605+
i16::min_value()
606+
} else {
607+
exp.try_into().unwrap()
608+
};
609+
let res = x.scalbn(exp);
601610
this.write_scalar(Scalar::from_f64(res), dest)?;
602611
}
603612

0 commit comments

Comments
 (0)