From 78dbd5c6ee09cd459d9eb8c86845b59c271866e7 Mon Sep 17 00:00:00 2001 From: Ryan Prichard Date: Fri, 7 Feb 2025 16:41:06 -0800 Subject: SafeMul: avoid UB on signed overflow Bug: http://b/395138850 Test: atest art-run-test-530-checker-loops4 Change-Id: Id7fb236cc4cbc7982de1a8e79f94814f9daf2bd1 --- compiler/optimizing/induction_var_range.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'compiler/optimizing/induction_var_range.cc') diff --git a/compiler/optimizing/induction_var_range.cc b/compiler/optimizing/induction_var_range.cc index 2f5a22ec0e..1fa2724641 100644 --- a/compiler/optimizing/induction_var_range.cc +++ b/compiler/optimizing/induction_var_range.cc @@ -48,10 +48,11 @@ static bool IsSafeDiv(int32_t c1, int32_t c2) { /** Computes a * b for a,b > 0 (at least until first overflow happens). */ static int64_t SafeMul(int64_t a, int64_t b, /*out*/ bool* overflow) { - if (a > 0 && b > 0 && a > (std::numeric_limits::max() / b)) { + int64_t result; + if (__builtin_mul_overflow(a, b, &result)) { *overflow = true; } - return a * b; + return result; } /** Returns b^e for b,e > 0. Sets overflow if arithmetic wrap-around occurred. */ -- cgit v1.2.3-59-g8ed1b