ICU-20079 integer overflow & divide by zero sanitizer fixes. (#67)

This commit is contained in:
Andy Heninger 2018-08-20 16:40:46 -07:00 committed by Shane Carr
parent 3420d5a55b
commit 278b9cb735
No known key found for this signature in database
GPG Key ID: FCED3B24AAB18B5C
6 changed files with 23 additions and 19 deletions

View File

@ -339,7 +339,8 @@ BytesTrieBuilder::indexOfElementWithNextUnit(int32_t i, int32_t byteIndex, UChar
BytesTrieBuilder::BTLinearMatchNode::BTLinearMatchNode(const char *bytes, int32_t len, Node *nextNode)
: LinearMatchNode(len, nextNode), s(bytes) {
hash=hash*37+ustr_hashCharsN(bytes, len);
hash=static_cast<int32_t>(
static_cast<uint32_t>(hash)*37u + static_cast<uint32_t>(ustr_hashCharsN(bytes, len)));
}
UBool

View File

@ -364,12 +364,12 @@ UBool UnicodeSet::operator==(const UnicodeSet& o) const {
* @see Object#hashCode()
*/
int32_t UnicodeSet::hashCode(void) const {
int32_t result = len;
uint32_t result = static_cast<uint32_t>(len);
for (int32_t i = 0; i < len; ++i) {
result *= 1000003;
result *= 1000003u;
result += list[i];
}
return result;
return static_cast<int32_t>(result);
}
//----------------------------------------------------------------

View File

@ -112,7 +112,9 @@ hashEntry(const UHashTok parm) {
UHashTok namekey, pathkey;
namekey.pointer = b->name;
pathkey.pointer = b->path;
return uhash_hashChars(namekey)+37*uhash_hashChars(pathkey);
uint32_t unsignedHash = static_cast<uint32_t>(uhash_hashChars(namekey)) +
37u * static_cast<uint32_t>(uhash_hashChars(pathkey));
return static_cast<int32_t>(unsignedHash);
}
/* compares two entries */

View File

@ -140,7 +140,7 @@ ufmt_uto64(const UChar *buffer,
{
const UChar *limit;
int32_t count;
int64_t result;
uint64_t result;
/* intialize parameters */
@ -160,7 +160,7 @@ ufmt_uto64(const UChar *buffer,
}
*len = count;
return result;
return static_cast<int64_t>(result);
}
#define NIBBLE_PER_BYTE 2

View File

@ -11,6 +11,7 @@
#include "numrgts.h"
#include <cmath> // std::signbit
#include <float.h> // DBL_MIN, DBL_MAX
#include <stdio.h>
@ -2142,8 +2143,9 @@ NumberFormatRegressionTest::Test4162852(void)
{
UErrorCode status = U_ZERO_ERROR;
for(int32_t i=0; i < 2; ++i) {
NumberFormat *f = (i == 0) ? NumberFormat::createInstance(status)
: NumberFormat::createPercentInstance(status);
LocalPointer<NumberFormat> f(
((i == 0) ? NumberFormat::createInstance(status) : NumberFormat::createPercentInstance(status)),
status);
if(U_FAILURE(status)) {
dataerrln("Couldn't create number format - %s", u_errorName(status));
return;
@ -2154,20 +2156,19 @@ NumberFormatRegressionTest::Test4162852(void)
f->format(d, s);
Formattable n;
f->parse(s, n, status);
if(U_FAILURE(status))
if(U_FAILURE(status)) {
errln("Couldn't parse!");
return;
}
double e = n.getDouble();
logln(UnicodeString("") +
d + " -> " +
'"' + s + '"' + " -> " + e);
logln("%f -> \"%s\" -> %f", d, CStr(s)(), e);
#if (U_PLATFORM == U_PF_OS390 && !defined(IEEE_754)) || U_PLATFORM == U_PF_OS400
if (e != 0.0) {
#else
if (e != 0.0 || 1.0/e > 0.0) {
if (e != 0.0 || (std::signbit(e) == false)) {
#endif
logln("Failed to parse negative zero");
errln("Failed to parse negative zero");
}
delete f;
}
}

View File

@ -178,18 +178,18 @@ char* DataDrivenLogger::fgTestDataPath = NULL;
static int64_t
uto64(const UChar *buffer)
{
int64_t result = 0;
uint64_t result = 0;
/* iterate through buffer */
while(*buffer) {
/* read the next digit */
result *= 16;
result *= 16u;
if (!u_isxdigit(*buffer)) {
log_err("\\u%04X is not a valid hex digit for this test\n", (UChar)*buffer);
}
result += *buffer - 0x0030 - (*buffer >= 0x0041 ? (*buffer >= 0x0061 ? 39 : 7) : 0);
buffer++;
}
return result;
return (int64_t)result;
}
#endif