]> git.lizzy.rs Git - rust.git/commitdiff
clamp ldexp exponent to i16
authorRalf Jung <post@ralfj.de>
Sat, 10 Aug 2019 09:27:27 +0000 (11:27 +0200)
committerRalf Jung <post@ralfj.de>
Sat, 10 Aug 2019 09:27:27 +0000 (11:27 +0200)
src/shims/foreign_items.rs

index add6bd5bbefd6620e897c28a687a97e2fb475760..4cca6b9efdd12ce124fd16e66ac88ae20c4beadd 100644 (file)
@@ -597,7 +597,16 @@ fn emulate_foreign_item(
             "_ldexp" | "ldexp" | "scalbn" => {
                 let x = this.read_scalar(args[0])?.to_f64()?;
                 let exp = this.read_scalar(args[1])?.to_i32()?;
-                let res = x.scalbn(exp.try_into().unwrap());
+                // Saturating cast to i16. Even those are outside the valid exponent range to
+                // `scalbn` below will to its over/underflow handling.
+                let exp = if exp > i16::max_value() as i32 {
+                    i16::max_value()
+                } else if exp < i16::min_value() as i32 {
+                    i16::min_value()
+                } else {
+                    exp.try_into().unwrap()
+                };
+                let res = x.scalbn(exp);
                 this.write_scalar(Scalar::from_f64(res), dest)?;
             }