Commit 29b6c9b7 authored by abolz's avatar abolz

Add assertions to check preconditions of functions and unsigned integer arithmetic

parent 80dba56a
...@@ -141,6 +141,9 @@ struct DiyFp { ...@@ -141,6 +141,9 @@ struct DiyFp {
double d; double d;
uint64_t u64; uint64_t u64;
}u; }u;
RAPIDJSON_ASSERT(f <= kDpHiddenBit + kDpSignificandMask);
RAPIDJSON_ASSERT(e >= kDpDenormalExponent);
RAPIDJSON_ASSERT(e < kDpMaxExponent);
const uint64_t be = (e == kDpDenormalExponent && (f & kDpHiddenBit) == 0) ? 0 : const uint64_t be = (e == kDpDenormalExponent && (f & kDpHiddenBit) == 0) ? 0 :
static_cast<uint64_t>(e + kDpExponentBias); static_cast<uint64_t>(e + kDpExponentBias);
u.u64 = (f & kDpSignificandMask) | (be << kDpSignificandSize); u.u64 = (f & kDpSignificandMask) | (be << kDpSignificandSize);
...@@ -220,6 +223,7 @@ inline DiyFp GetCachedPowerByIndex(size_t index) { ...@@ -220,6 +223,7 @@ inline DiyFp GetCachedPowerByIndex(size_t index) {
641, 667, 694, 720, 747, 774, 800, 827, 853, 880, 641, 667, 694, 720, 747, 774, 800, 827, 853, 880,
907, 933, 960, 986, 1013, 1039, 1066 907, 933, 960, 986, 1013, 1039, 1066
}; };
RAPIDJSON_ASSERT(index < 87);
return DiyFp(kCachedPowers_F[index], kCachedPowers_E[index]); return DiyFp(kCachedPowers_F[index], kCachedPowers_E[index]);
} }
...@@ -238,10 +242,11 @@ inline DiyFp GetCachedPower(int e, int* K) { ...@@ -238,10 +242,11 @@ inline DiyFp GetCachedPower(int e, int* K) {
} }
inline DiyFp GetCachedPower10(int exp, int *outExp) { inline DiyFp GetCachedPower10(int exp, int *outExp) {
unsigned index = (static_cast<unsigned>(exp) + 348u) / 8u; RAPIDJSON_ASSERT(exp >= -348);
*outExp = -348 + static_cast<int>(index) * 8; unsigned index = static_cast<unsigned>(exp + 348) / 8u;
return GetCachedPowerByIndex(index); *outExp = -348 + static_cast<int>(index) * 8;
} return GetCachedPowerByIndex(index);
}
#ifdef __GNUC__ #ifdef __GNUC__
RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_POP
......
...@@ -233,12 +233,14 @@ inline double StrtodFullPrecision(double d, int p, const char* decimals, size_t ...@@ -233,12 +233,14 @@ inline double StrtodFullPrecision(double d, int p, const char* decimals, size_t
while (*decimals == '0' && length > 1) { while (*decimals == '0' && length > 1) {
length--; length--;
decimals++; decimals++;
RAPIDJSON_ASSERT(decimalPosition > 0);
decimalPosition--; decimalPosition--;
} }
// Trim trailing zeros // Trim trailing zeros
while (decimals[length - 1] == '0' && length > 1) { while (decimals[length - 1] == '0' && length > 1) {
length--; length--;
RAPIDJSON_ASSERT(decimalPosition > 0);
decimalPosition--; decimalPosition--;
exp++; exp++;
} }
...@@ -248,6 +250,7 @@ inline double StrtodFullPrecision(double d, int p, const char* decimals, size_t ...@@ -248,6 +250,7 @@ inline double StrtodFullPrecision(double d, int p, const char* decimals, size_t
if (static_cast<int>(length) > kMaxDecimalDigit) { if (static_cast<int>(length) > kMaxDecimalDigit) {
int delta = (static_cast<int>(length) - kMaxDecimalDigit); int delta = (static_cast<int>(length) - kMaxDecimalDigit);
exp += delta; exp += delta;
RAPIDJSON_ASSERT(decimalPosition > static_cast<unsigned>(delta));
decimalPosition -= static_cast<unsigned>(delta); decimalPosition -= static_cast<unsigned>(delta);
length = kMaxDecimalDigit; length = kMaxDecimalDigit;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment