diff options
| author | HarmoGlace <23212967+HarmoGlace@users.noreply.github.com> | 2023-04-26 11:30:16 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-26 11:30:16 +0200 |
| commit | 7cb63d1aae349541aabc165d8c9e5c0943e38511 (patch) | |
| tree | 8d106aa4c3f20756c42d2ebebba60efa6fda0265 /src | |
| parent | a6df909a8d8018ef87b66b2128c6acfa9fb0599c (diff) | |
Fix overflows with operators (#904)
Diffstat (limited to 'src')
| -rw-r--r-- | src/eval/ops.rs | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/src/eval/ops.rs b/src/eval/ops.rs index 43ee6ceb..48e74c8f 100644 --- a/src/eval/ops.rs +++ b/src/eval/ops.rs @@ -53,7 +53,7 @@ pub fn pos(value: Value) -> StrResult<Value> { /// Compute the negation of a value. pub fn neg(value: Value) -> StrResult<Value> { Ok(match value { - Int(v) => Int(-v), + Int(v) => Int(v.checked_neg().ok_or("value is too large")?), Float(v) => Float(-v), Length(v) => Length(-v), Angle(v) => Angle(-v), @@ -70,7 +70,7 @@ pub fn add(lhs: Value, rhs: Value) -> StrResult<Value> { (a, None) => a, (None, b) => b, - (Int(a), Int(b)) => Int(a + b), + (Int(a), Int(b)) => Int(a.checked_add(b).ok_or("value is too large")?), (Int(a), Float(b)) => Float(a as f64 + b), (Float(a), Int(b)) => Float(a + b as f64), (Float(a), Float(b)) => Float(a + b), @@ -137,7 +137,7 @@ pub fn add(lhs: Value, rhs: Value) -> StrResult<Value> { /// Compute the difference of two values. pub fn sub(lhs: Value, rhs: Value) -> StrResult<Value> { Ok(match (lhs, rhs) { - (Int(a), Int(b)) => Int(a - b), + (Int(a), Int(b)) => Int(a.checked_sub(b).ok_or("value is too large")?), (Int(a), Float(b)) => Float(a as f64 - b), (Float(a), Int(b)) => Float(a - b as f64), (Float(a), Float(b)) => Float(a - b), @@ -165,7 +165,7 @@ pub fn sub(lhs: Value, rhs: Value) -> StrResult<Value> { /// Compute the product of two values. pub fn mul(lhs: Value, rhs: Value) -> StrResult<Value> { Ok(match (lhs, rhs) { - (Int(a), Int(b)) => Int(a * b), + (Int(a), Int(b)) => Int(a.checked_mul(b).ok_or("value is too large")?), (Int(a), Float(b)) => Float(a as f64 * b), (Float(a), Int(b)) => Float(a * b as f64), (Float(a), Float(b)) => Float(a * b), |
