From 50471ccd4438a5e4aa36860b9c6226af2bc59c7c Mon Sep 17 00:00:00 2001 From: Arnav Kulshreshtha Date: Sat, 21 Feb 2026 04:52:33 +0530 Subject: [PATCH] Fix intermediate integer overflow in math.lcm and math.hypot --- src/runtime/math.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/runtime/math.py b/src/runtime/math.py index 8993ce1223..1fa3388f7f 100644 --- a/src/runtime/math.py +++ b/src/runtime/math.py @@ -498,9 +498,9 @@ def lcm(a: i32, b: i32) -> i32: a_ = -a_ if b_ < 0: b_ = -b_ - if a_*b_ == 0: + if a_ == 0 or b_ == 0: return 0 - return i32((a_*b_)//gcd(a_, b_)) + return i32((a_ // gcd(a_, b_)) * b_) def copysign(x: f64, y: f64) -> f64: @@ -517,7 +517,11 @@ def hypot(x: i32, y: i32) -> f64: """ Returns the hypotenuse of the right triangle with sides `x` and `y`. """ - return sqrt(f64(1.0)*f64(x**2 + y**2)) + xf: f64 + yf: f64 + xf = f64(x) + yf = f64(y) + return sqrt(xf**2.0 + yf**2.0) @overload def trunc(x: f64) -> i64: