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 {
double d;
uint64_t u64;
}u;
RAPIDJSON_ASSERT(f <= kDpHiddenBit + kDpSignificandMask);
RAPIDJSON_ASSERT(e >= kDpDenormalExponent);
RAPIDJSON_ASSERT(e < kDpMaxExponent);
const uint64_t be = (e == kDpDenormalExponent && (f & kDpHiddenBit) == 0) ? 0 :
static_cast<uint64_t>(e + kDpExponentBias);
u.u64 = (f & kDpSignificandMask) | (be << kDpSignificandSize);
......@@ -220,6 +223,7 @@ inline DiyFp GetCachedPowerByIndex(size_t index) {
641, 667, 694, 720, 747, 774, 800, 827, 853, 880,
907, 933, 960, 986, 1013, 1039, 1066
};
RAPIDJSON_ASSERT(index < 87);
return DiyFp(kCachedPowers_F[index], kCachedPowers_E[index]);
}
......@@ -238,10 +242,11 @@ inline DiyFp GetCachedPower(int e, int* K) {
}
inline DiyFp GetCachedPower10(int exp, int *outExp) {
unsigned index = (static_cast<unsigned>(exp) + 348u) / 8u;
*outExp = -348 + static_cast<int>(index) * 8;
return GetCachedPowerByIndex(index);
}
RAPIDJSON_ASSERT(exp >= -348);
unsigned index = static_cast<unsigned>(exp + 348) / 8u;
*outExp = -348 + static_cast<int>(index) * 8;
return GetCachedPowerByIndex(index);
}
#ifdef __GNUC__
RAPIDJSON_DIAG_POP
......
......@@ -233,12 +233,14 @@ inline double StrtodFullPrecision(double d, int p, const char* decimals, size_t
while (*decimals == '0' && length > 1) {
length--;
decimals++;
RAPIDJSON_ASSERT(decimalPosition > 0);
decimalPosition--;
}
// Trim trailing zeros
while (decimals[length - 1] == '0' && length > 1) {
length--;
RAPIDJSON_ASSERT(decimalPosition > 0);
decimalPosition--;
exp++;
}
......@@ -248,6 +250,7 @@ inline double StrtodFullPrecision(double d, int p, const char* decimals, size_t
if (static_cast<int>(length) > kMaxDecimalDigit) {
int delta = (static_cast<int>(length) - kMaxDecimalDigit);
exp += delta;
RAPIDJSON_ASSERT(decimalPosition > static_cast<unsigned>(delta));
decimalPosition -= static_cast<unsigned>(delta);
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