From 32a23a9bb5b1e8b29a5acc13da5875457bd67e61 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Tue, 20 Oct 2026 14:17:58 +1100 Subject: [PATCH] base: backport parse_number_{u64,f64} fixes This backports commits 9d41525a2f4c6b3f99739b72c43ef57be57b0215 and c99f5af9fe1f5cbcf3662a04d5264f2488ff4b89 from the main branch (working on an unstable v0.4) to the v0.3 branch. --- internal/cgen/base/floatconv-submodule-code.c | 39 ++++++++---------- internal/cgen/base/intconv-submodule.c | 2 + release/c/wuffs-unsupported-snapshot.c | 41 +++++++++---------- 3 files changed, 40 insertions(+), 42 deletions(-) diff --git a/internal/cgen/base/floatconv-submodule-code.c b/internal/cgen/base/floatconv-submodule-code.c index ca109643..4357c15d 100644 --- a/internal/cgen/base/floatconv-submodule-code.c +++ b/internal/cgen/base/floatconv-submodule-code.c @@ -276,14 +276,12 @@ wuffs_base__private_implementation__high_prec_dec__parse( uint8_t* q = s.ptr + s.len; if (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES) { - for (;; p++) { - if (p >= q) { - return wuffs_base__make_status(wuffs_base__error__bad_argument); - } else if (*p != '_') { - break; - } + for (; (p < q) && (*p == '_'); p++) { } } + if (p >= q) { + return wuffs_base__make_status(wuffs_base__error__bad_argument); + } // Parse sign. do { @@ -296,14 +294,12 @@ wuffs_base__private_implementation__high_prec_dec__parse( break; } if (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES) { - for (;; p++) { - if (p >= q) { - return wuffs_base__make_status(wuffs_base__error__bad_argument); - } else if (*p != '_') { - break; - } + for (; (p < q) && (*p == '_'); p++) { } } + if (p >= q) { + return wuffs_base__make_status(wuffs_base__error__bad_argument); + } } while (0); // Parse digits, up to (and including) a '.', 'E' or 'e'. Examples for each @@ -355,9 +351,12 @@ wuffs_base__private_implementation__high_prec_dec__parse( if (nd < WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) { h->digits[nd++] = (uint8_t)(*p - '0'); dp = (int32_t)nd; - } else if ('0' != *p) { - // Long-tail non-zeroes set the truncated bit. - h->truncated = true; + } else { + dp++; + if ('0' != *p) { + // Long-tail non-zeroes set the truncated bit. + h->truncated = true; + } } } else if (*p == ((options & @@ -417,14 +416,12 @@ after_sep: after_exp: do { if (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES) { - for (;; p++) { - if (p >= q) { - return wuffs_base__make_status(wuffs_base__error__bad_argument); - } else if (*p != '_') { - break; - } + for (; (p < q) && (*p == '_'); p++) { } } + if (p >= q) { + return wuffs_base__make_status(wuffs_base__error__bad_argument); + } int32_t exp_sign = +1; if (*p == '+') { diff --git a/internal/cgen/base/intconv-submodule.c b/internal/cgen/base/intconv-submodule.c index c0632674..0e1c18a2 100644 --- a/internal/cgen/base/intconv-submodule.c +++ b/internal/cgen/base/intconv-submodule.c @@ -219,6 +219,7 @@ wuffs_base__parse_number_u64(wuffs_base__slice_u8 s, uint32_t options) { if (p < q) { goto hexadecimal; } + goto fail_bad_argument; } else if ((*p == 'd') || (*p == 'D')) { p++; @@ -229,6 +230,7 @@ wuffs_base__parse_number_u64(wuffs_base__slice_u8 s, uint32_t options) { if (p < q) { goto decimal; } + goto fail_bad_argument; } if (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_MULTIPLE_LEADING_ZEROES) { diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 8c4a0a0f..267584fe 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -13793,14 +13793,12 @@ wuffs_base__private_implementation__high_prec_dec__parse( uint8_t* q = s.ptr + s.len; if (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES) { - for (;; p++) { - if (p >= q) { - return wuffs_base__make_status(wuffs_base__error__bad_argument); - } else if (*p != '_') { - break; - } + for (; (p < q) && (*p == '_'); p++) { } } + if (p >= q) { + return wuffs_base__make_status(wuffs_base__error__bad_argument); + } // Parse sign. do { @@ -13813,14 +13811,12 @@ wuffs_base__private_implementation__high_prec_dec__parse( break; } if (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES) { - for (;; p++) { - if (p >= q) { - return wuffs_base__make_status(wuffs_base__error__bad_argument); - } else if (*p != '_') { - break; - } + for (; (p < q) && (*p == '_'); p++) { } } + if (p >= q) { + return wuffs_base__make_status(wuffs_base__error__bad_argument); + } } while (0); // Parse digits, up to (and including) a '.', 'E' or 'e'. Examples for each @@ -13872,9 +13868,12 @@ wuffs_base__private_implementation__high_prec_dec__parse( if (nd < WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) { h->digits[nd++] = (uint8_t)(*p - '0'); dp = (int32_t)nd; - } else if ('0' != *p) { - // Long-tail non-zeroes set the truncated bit. - h->truncated = true; + } else { + dp++; + if ('0' != *p) { + // Long-tail non-zeroes set the truncated bit. + h->truncated = true; + } } } else if (*p == ((options & @@ -13934,14 +13933,12 @@ after_sep: after_exp: do { if (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES) { - for (;; p++) { - if (p >= q) { - return wuffs_base__make_status(wuffs_base__error__bad_argument); - } else if (*p != '_') { - break; - } + for (; (p < q) && (*p == '_'); p++) { } } + if (p >= q) { + return wuffs_base__make_status(wuffs_base__error__bad_argument); + } int32_t exp_sign = +1; if (*p == '+') { @@ -15750,6 +15747,7 @@ wuffs_base__parse_number_u64(wuffs_base__slice_u8 s, uint32_t options) { if (p < q) { goto hexadecimal; } + goto fail_bad_argument; } else if ((*p == 'd') || (*p == 'D')) { p++; @@ -15760,6 +15758,7 @@ wuffs_base__parse_number_u64(wuffs_base__slice_u8 s, uint32_t options) { if (p < q) { goto decimal; } + goto fail_bad_argument; } if (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_MULTIPLE_LEADING_ZEROES) {