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

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