Commit 83eed255aa for openssl.org
commit 83eed255aa534700df51dadb2c5ff8677bbdd831
Author: Paul Dale <paul.dale@oracle.com>
Date: Mon Nov 23 13:00:29 2026 +1100
include/internal/safe_math.h: fix unsigned multiplication overflow results
Return the result already produced by the overflow builtin.
In the portable helper, keep the product unsigned when narrow inputs
promote to int so that an overflowing product remains defined.
Fixes: b037e3637a49 "header: add integer overflow helper functions"
Assisted-by: ChatGPT:gpt-6-Sol
Reviewed-by: Eugene Syromiatnikov <esyr@openssl.org>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Merge-date: Tue Nov 24 18:13:15 2026
Merged-from: https://github.com/openssl/openssl/pull/33917
diff --git a/include/internal/safe_math.h b/include/internal/safe_math.h
index 42b9076f75..b24502803f 100644
--- a/include/internal/safe_math.h
+++ b/include/internal/safe_math.h
@@ -154,7 +154,7 @@
if (!__builtin_mul_overflow(a, b, &r)) \
return r; \
*err |= 1; \
- return a * b; \
+ return r; \
}
#else /* has(__builtin_mul_overflow) */
@@ -187,7 +187,12 @@
{ \
if (b != 0 && a > max / b) \
*err |= 1; \
- return a * b; \
+ /* \
+ * With 16-bit short and 32-bit int, unsigned short \
+ * operands of 65535 promote to int and overflow when \
+ * multiplied. Adding 0U forces unsigned arithmetic. \
+ */ \
+ return a * (b + 0U); \
}
#endif /* has(__builtin_mul_overflow) */