From d83d2ba26087305dd044e8d011b775f4a4b89930 Mon Sep 17 00:00:00 2001 From: abolz Date: Fri, 15 Jun 2018 10:46:45 +0200 Subject: [PATCH] Trim all zeros from input If the buffer only contains zeros, return 0. --- include/rapidjson/internal/strtod.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/rapidjson/internal/strtod.h b/include/rapidjson/internal/strtod.h index 5f402d9..7826a8b 100644 --- a/include/rapidjson/internal/strtod.h +++ b/include/rapidjson/internal/strtod.h @@ -242,17 +242,21 @@ inline double StrtodFullPrecision(double d, int p, const char* decimals, size_t RAPIDJSON_ASSERT(dExp <= INT_MAX - dLen); // Trim leading zeros - while (*decimals == '0' && dLen > 1) { + while (dLen > 0 && *decimals == '0') { dLen--; decimals++; } // Trim trailing zeros - while (decimals[dLen - 1] == '0' && dLen > 1) { + while (dLen > 0 && decimals[dLen - 1] == '0') { dLen--; dExp++; } + if (dLen == 0) { // Buffer only contains zeros. + return 0.0; + } + // Trim right-most digits const int kMaxDecimalDigit = 780; if (dLen > kMaxDecimalDigit) {