]> granicus.if.org Git - icu/commitdiff
ICU-12572 Fixing some undefined behavior warnings in number code.
authorShane Carr <shane@unicode.org>
Tue, 8 May 2018 07:59:41 +0000 (07:59 +0000)
committerShane Carr <shane@unicode.org>
Tue, 8 May 2018 07:59:41 +0000 (07:59 +0000)
X-SVN-Rev: 41358

icu4c/source/i18n/decimfmt.cpp
icu4c/source/i18n/number_decimfmtprops.cpp

index 8e37bd2e84e6fe82225e345764481433d7b6cd43..16412535a85c9949eda50f483d91dd5bd1ef384d 100644 (file)
@@ -1317,11 +1317,13 @@ bool DecimalFormat::fastFormatDouble(double input, UnicodeString& output) const
     if (!fields->canUseFastFormat) {
         return false;
     }
-    auto i32 = static_cast<int32_t>(input);
-    if (i32 != input || i32 == INT32_MIN) {
+    if (std::isnan(input)
+            || std::trunc(input) != input
+            || input <= INT32_MIN
+            || input > INT32_MAX) {
         return false;
     }
-    doFastFormatInt32(i32, std::signbit(input), output);
+    doFastFormatInt32(static_cast<int32_t>(input), std::signbit(input), output);
     return true;
 }
 
@@ -1329,11 +1331,10 @@ bool DecimalFormat::fastFormatInt64(int64_t input, UnicodeString& output) const
     if (!fields->canUseFastFormat) {
         return false;
     }
-    auto i32 = static_cast<int32_t>(input);
-    if (i32 != input || i32 == INT32_MIN) {
+    if (input <= INT32_MIN || input > INT32_MAX) {
         return false;
     }
-    doFastFormatInt32(i32, input < 0, output);
+    doFastFormatInt32(static_cast<int32_t>(input), input < 0, output);
     return true;
 }
 
index c61e06c6506e80749f02a11624893eb8eeac2d86..6754fe19eca56cffbc6861ffd7d1fefed40eb4ab 100644 (file)
@@ -20,7 +20,7 @@ char kRawDefaultProperties[sizeof(DecimalFormatProperties)];
 icu::UInitOnce gDefaultPropertiesInitOnce = U_INITONCE_INITIALIZER;
 
 void U_CALLCONV initDefaultProperties(UErrorCode&) {
-    *reinterpret_cast<DecimalFormatProperties*>(kRawDefaultProperties) = {}; // set to the default instance
+    new(kRawDefaultProperties) DecimalFormatProperties(); // set to the default instance
 }
 
 }