]> granicus.if.org Git - icu/commitdiff
ICU-12014 fix float-cast-overflow in rbnf.cpp
authorAndy Heninger <andy.heninger@gmail.com>
Wed, 2 Dec 2015 22:31:56 +0000 (22:31 +0000)
committerAndy Heninger <andy.heninger@gmail.com>
Wed, 2 Dec 2015 22:31:56 +0000 (22:31 +0000)
X-SVN-Rev: 38102

icu4c/source/i18n/rbnf.cpp

index de260118fa8f81649daabbe40ea25d9de29509d7..94f7e329f0852f5a5ad8c5ea7486cad56ffda1a1 100644 (file)
@@ -1260,9 +1260,12 @@ RuleBasedNumberFormat::parse(const UnicodeString& text,
     }
     result = high_result;
     if (result.getType() == Formattable::kDouble) {
-        int32_t r = (int32_t)result.getDouble();
-        if ((double)r == result.getDouble()) {
-            result.setLong(r);
+        double d = result.getDouble();
+        if (!uprv_isNaN(d) && d == uprv_trunc(d) && INT32_MIN <= d && d <= INT32_MAX) {
+            // Note: casting a double to an int when the double is too large or small
+            //       to fit the destination is undefined behavior. The explicit range checks,
+            //       above, are required. Just casting and checking the result value is undefined.
+            result.setLong(static_cast<int32_t>(d));
         }
     }
 }