From ce1996f1a7227493c99d67c82eab8d9ea09b5398 Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Thu, 21 May 2020 17:53:46 +0200 Subject: [PATCH] Correct float parsing when exponent has sign --- lib/chibi/json.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/chibi/json.c b/lib/chibi/json.c index 90e915b4..f083dec1 100644 --- a/lib/chibi/json.c +++ b/lib/chibi/json.c @@ -17,7 +17,7 @@ sexp sexp_json_exception (sexp ctx, sexp self, const char* msg, sexp str, const sexp parse_json_number (sexp ctx, sexp self, sexp str, const char* s, int* i, const int len) { double res = 0, scale = 1; - int j = *i, sign = 1, inexactp = 0; + int j = *i, sign = 1, inexactp = 0, scale_sign = 1; if (s[j] == '+') { ++j; } else if (s[j] == '-') { @@ -33,9 +33,17 @@ sexp parse_json_number (sexp ctx, sexp self, sexp str, const char* s, int* i, co res /= scale; } else if (j < len && sexp_tolower(s[j]) == 'e') { inexactp = 1; + if (j+1 < len) { + if (s[j+1] == '+') { + ++j; + } else if (s[j+1] == '-') { + ++j; + scale_sign = -1; + } + } for (++j, scale=0; j < len && isdigit(s[j]); ) scale = scale * 10 + s[j++] - '0'; - res *= pow(10.0, scale); + res *= pow(10.0, scale_sign * scale); } *i = j; return (inexactp || fabs(res) > SEXP_MAX_FIXNUM) ?