From cdff63405771982166e5ce0f9f343d8b8b1a92d5 Mon Sep 17 00:00:00 2001 From: Weaselbot Date: Sun, 19 Jul 2026 22:00:15 -0400 Subject: [PATCH] Fix schemagen integer parser rejecting zero with negative exponents Move the zero-detection check in the generated parseJsonInt64 before the finalExp < 0 guard. Previously, valid JSON numbers whose mathematical value is 0 but written with a large negative exponent (e.g. 0e-2, 0.0e-2, -0e-2, 0e-20) were rejected because the negative-finalExp early return ran before the all-zero-digits branch could set out = 0. Non-zero values with negative exponents are still correctly rejected. Closes #56 --- contrib/schemagen/test_schemagen.py | 10 ++++++++++ contrib/schemagen/weaseljson_schemagen.py | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/contrib/schemagen/test_schemagen.py b/contrib/schemagen/test_schemagen.py index a3e4425..4b4eed9 100644 --- a/contrib/schemagen/test_schemagen.py +++ b/contrib/schemagen/test_schemagen.py @@ -592,6 +592,13 @@ class SchemagenIntegerBoundaryTest(unittest.TestCase): ("123.0", "WeaselJson_OK", 123), ("9e18", "WeaselJson_OK", 9000000000000000000), ("10e18", "WeaselJson_REJECT", 0), + # Issue #56: zero written with a negative exponent must be accepted. + ("0e-1", "WeaselJson_OK", 0), + ("0e-2", "WeaselJson_OK", 0), + ("0.0e-2", "WeaselJson_OK", 0), + ("-0e-2", "WeaselJson_OK", 0), + ("0e-20", "WeaselJson_OK", 0), + ("0.000e-5", "WeaselJson_OK", 0), ] cases_src = self._build_cases_array("root", cases) harness = textwrap.dedent( @@ -634,6 +641,9 @@ class SchemagenIntegerBoundaryTest(unittest.TestCase): ('{"age":2.0}', "WeaselJson_OK", 2), ('{"age":0.0001e4}', "WeaselJson_OK", 1), ('{"age":0.001}', "WeaselJson_REJECT", 0), + # Issue #56: zero written with a negative exponent must be accepted. + ('{"age":0e-2}', "WeaselJson_OK", 0), + ('{"age":-0e-20}', "WeaselJson_OK", 0), ( '{"age":-9223372036854775808.0}', "WeaselJson_OK", diff --git a/contrib/schemagen/weaseljson_schemagen.py b/contrib/schemagen/weaseljson_schemagen.py index 2283454..32ae08e 100644 --- a/contrib/schemagen/weaseljson_schemagen.py +++ b/contrib/schemagen/weaseljson_schemagen.py @@ -1058,10 +1058,10 @@ private: ++trim; }} int64_t finalExp = exp - fracDigits + trim; - if (finalExp < 0) return false; size_t leadingZeros = 0; while (leadingZeros < digits.size() && digits[leadingZeros] == '0') ++leadingZeros; if (leadingZeros == digits.size()) {{ out = 0; return true; }} + if (finalExp < 0) return false; if (leadingZeros > 0) digits.erase(0, leadingZeros); constexpr uint64_t kMaxNeg = 9223372036854775808ULL;