summaryrefslogtreecommitdiff
path: root/compiler
diff options
context:
space:
mode:
author Ryan Prichard <rprichard@google.com> 2025-02-07 16:41:06 -0800
committer Pirama Arumuga Nainar <pirama@google.com> 2025-02-10 11:30:42 -0800
commit78dbd5c6ee09cd459d9eb8c86845b59c271866e7 (patch)
treed940225dfae7a0a38f1ae390e36fbc1e0dd1173e /compiler
parentd6d5643407ceb14bf275c14a78f94048b1992af5 (diff)
SafeMul: avoid UB on signed overflow
Bug: http://b/395138850 Test: atest art-run-test-530-checker-loops4 Change-Id: Id7fb236cc4cbc7982de1a8e79f94814f9daf2bd1
Diffstat (limited to 'compiler')
-rw-r--r--compiler/optimizing/induction_var_range.cc5
1 files changed, 3 insertions, 2 deletions
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<int64_t>::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. */