ICU-12572 Fixing some undefined behavior warnings in number code.

X-SVN-Rev: 41358
This commit is contained in:
Shane Carr 2018-05-08 07:59:41 +00:00
parent 8db1bee493
commit 71baac1713
2 changed files with 8 additions and 7 deletions

View 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;
}

View 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
}
}