Blame SOURCES/ICU-13634-Adding-integer-overflow-logic-to-ICU4C-num.patch

1dde6b
From 23d76d88630ecee02515e2c8f5c8769cc795ae23 Mon Sep 17 00:00:00 2001
1dde6b
From: Shane Carr <shane@unicode.org>
1dde6b
Date: Fri, 23 Mar 2018 00:56:16 +0000
1dde6b
Subject: [PATCH] ICU-13634 Adding integer overflow logic to ICU4C number
1dde6b
 pipeline in places where it is in ICU4J.
1dde6b
1dde6b
X-SVN-Rev: 41136
1dde6b
1dde6b
diff --git a/icu4c/source/common/putil.cpp b/icu4c/source/common/putil.cpp
1dde6b
index 83f08ac070..452e2fd79c 100644
1dde6b
--- a/icu4c/source/common/putil.cpp
1dde6b
+++ b/icu4c/source/common/putil.cpp
1dde6b
@@ -533,6 +533,30 @@ uprv_fmin(double x, double y)
1dde6b
     return (x > y ? y : x);
1dde6b
 }
1dde6b
 
1dde6b
+#include <iostream>
1dde6b
+
1dde6b
+U_CAPI UBool U_EXPORT2
1dde6b
+uprv_add32_overflow(int32_t a, int32_t b, int32_t* res) {
1dde6b
+    // NOTE: Some compilers (GCC, Clang) have primitives available, like __builtin_add_overflow.
1dde6b
+    // This function could be optimized by calling one of those primitives.
1dde6b
+    auto a64 = static_cast<int64_t>(a);
1dde6b
+    auto b64 = static_cast<int64_t>(b);
1dde6b
+    int64_t res64 = a64 + b64;
1dde6b
+    *res = static_cast<int32_t>(res64);
1dde6b
+    return res64 != *res;
1dde6b
+}
1dde6b
+
1dde6b
+U_CAPI UBool U_EXPORT2
1dde6b
+uprv_mul32_overflow(int32_t a, int32_t b, int32_t* res) {
1dde6b
+    // NOTE: Some compilers (GCC, Clang) have primitives available, like __builtin_mul_overflow.
1dde6b
+    // This function could be optimized by calling one of those primitives.
1dde6b
+    auto a64 = static_cast<int64_t>(a);
1dde6b
+    auto b64 = static_cast<int64_t>(b);
1dde6b
+    int64_t res64 = a64 * b64;
1dde6b
+    *res = static_cast<int32_t>(res64);
1dde6b
+    return res64 != *res;
1dde6b
+}
1dde6b
+
1dde6b
 /**
1dde6b
  * Truncates the given double.
1dde6b
  * trunc(3.3) = 3.0, trunc (-3.3) = -3.0
1dde6b
diff --git a/icu4c/source/common/putilimp.h b/icu4c/source/common/putilimp.h
1dde6b
index eb9b5380f1..8b858df9e3 100644
1dde6b
--- a/icu4c/source/common/putilimp.h
1dde6b
+++ b/icu4c/source/common/putilimp.h
1dde6b
@@ -391,6 +391,32 @@ U_INTERNAL double  U_EXPORT2 uprv_log(double d);
1dde6b
  */
1dde6b
 U_INTERNAL double  U_EXPORT2 uprv_round(double x);
1dde6b
 
1dde6b
+/**
1dde6b
+ * Adds the signed integers a and b, storing the result in res.
1dde6b
+ * Checks for signed integer overflow.
1dde6b
+ * Similar to the GCC/Clang extension __builtin_add_overflow
1dde6b
+ *
1dde6b
+ * @param a The first operand.
1dde6b
+ * @param b The second operand.
1dde6b
+ * @param res a + b
1dde6b
+ * @return true if overflow occurred; false if no overflow occurred.
1dde6b
+ * @internal
1dde6b
+ */
1dde6b
+U_INTERNAL UBool U_EXPORT2 uprv_add32_overflow(int32_t a, int32_t b, int32_t* res);
1dde6b
+
1dde6b
+/**
1dde6b
+ * Multiplies the signed integers a and b, storing the result in res.
1dde6b
+ * Checks for signed integer overflow.
1dde6b
+ * Similar to the GCC/Clang extension __builtin_mul_overflow
1dde6b
+ *
1dde6b
+ * @param a The first multiplicand.
1dde6b
+ * @param b The second multiplicand.
1dde6b
+ * @param res a * b
1dde6b
+ * @return true if overflow occurred; false if no overflow occurred.
1dde6b
+ * @internal
1dde6b
+ */
1dde6b
+U_INTERNAL UBool U_EXPORT2 uprv_mul32_overflow(int32_t a, int32_t b, int32_t* res);
1dde6b
+
1dde6b
 #if 0
1dde6b
 /**
1dde6b
  * Returns the number of digits after the decimal point in a double number x.
1dde6b
diff --git a/icu4c/source/test/cintltst/putiltst.c b/icu4c/source/test/cintltst/putiltst.c
1dde6b
index b99d9fca9c..1c3e073041 100644
1dde6b
--- a/icu4c/source/test/cintltst/putiltst.c
1dde6b
+++ b/icu4c/source/test/cintltst/putiltst.c
1dde6b
@@ -128,6 +128,20 @@ static void TestPUtilAPI(void){
1dde6b
         log_err("ERROR: uprv_isInfinite failed.\n");
1dde6b
     }
1dde6b
 
1dde6b
+    log_verbose("Testing the APIs uprv_add32_overflow and uprv_mul32_overflow\n");
1dde6b
+    int32_t overflow_result;
1dde6b
+    doAssert(FALSE, uprv_add32_overflow(INT32_MAX - 2, 1, &overflow_result), "should not overflow");
1dde6b
+    doAssert(INT32_MAX - 1, overflow_result, "should equal INT32_MAX - 1");
1dde6b
+    doAssert(FALSE, uprv_add32_overflow(INT32_MAX - 2, 2, &overflow_result), "should not overflow");
1dde6b
+    doAssert(INT32_MAX, overflow_result, "should equal exactly INT32_MAX");
1dde6b
+    doAssert(TRUE, uprv_add32_overflow(INT32_MAX - 2, 3, &overflow_result), "should overflow");
1dde6b
+    doAssert(FALSE, uprv_mul32_overflow(INT32_MAX / 5, 4, &overflow_result), "should not overflow");
1dde6b
+    doAssert(INT32_MAX / 5 * 4, overflow_result, "should equal INT32_MAX / 5 * 4");
1dde6b
+    doAssert(TRUE, uprv_mul32_overflow(INT32_MAX / 5, 6, &overflow_result), "should overflow");
1dde6b
+    // Test on negative numbers:
1dde6b
+    doAssert(FALSE, uprv_add32_overflow(-3, -2, &overflow_result), "should not overflow");
1dde6b
+    doAssert(-5, overflow_result, "should equal -5");
1dde6b
+
1dde6b
 #if 0
1dde6b
     log_verbose("Testing the API uprv_digitsAfterDecimal()....\n");
1dde6b
     doAssert(uprv_digitsAfterDecimal(value1), 3, "uprv_digitsAfterDecimal() failed.");
1dde6b
-- 
1dde6b
2.24.1
1dde6b