summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarmoGlace <23212967+HarmoGlace@users.noreply.github.com>2023-04-13 21:08:54 +0200
committerGitHub <noreply@github.com>2023-04-13 21:08:54 +0200
commit29b36d487609b6a291c3847703f36928082cc8a2 (patch)
tree1e684d67757a6b2885e1027d1e74a60912db0b48
parent6d596da72b16e486a4ca9c69a416bf21b88d30f0 (diff)
Fix `pow` overflow (#784)
-rw-r--r--library/src/compute/calc.rs6
-rw-r--r--tests/typ/compute/calc.typ4
2 files changed, 9 insertions, 1 deletions
diff --git a/library/src/compute/calc.rs b/library/src/compute/calc.rs
index f4695675..d5166449 100644
--- a/library/src/compute/calc.rs
+++ b/library/src/compute/calc.rs
@@ -108,7 +108,11 @@ pub fn pow(
};
let result = match (base, exponent.v) {
- (Num::Int(a), Num::Int(b)) if b >= 0 => Num::Int(a.pow(b as u32)),
+ (Num::Int(a), Num::Int(b)) if b >= 0 => a
+ .checked_pow(b as u32)
+ .map(Num::Int)
+ .ok_or("the result is too large")
+ .at(args.span)?,
(a, Num::Int(b)) => Num::Float(a.float().powi(b as i32)),
(a, b) => Num::Float(a.float().powf(b.float())),
};
diff --git a/tests/typ/compute/calc.typ b/tests/typ/compute/calc.typ
index 905520e6..c9e23542 100644
--- a/tests/typ/compute/calc.typ
+++ b/tests/typ/compute/calc.typ
@@ -91,6 +91,10 @@
#calc.pow(2, 10000000000000000)
---
+// Error: 10-25 the result is too large
+#calc.pow(2, 2147483647)
+
+---
// Error: 14-36 exponent may not be infinite, subnormal, or NaN
#calc.pow(2, calc.pow(2.0, 10000.0))