diff --git a/internal/cgen/base/floatconv-submodule-code.c b/internal/cgen/base/floatconv-submodule-code.c index 675c25c2..557cce7f 100644 --- a/internal/cgen/base/floatconv-submodule-code.c +++ b/internal/cgen/base/floatconv-submodule-code.c @@ -268,14 +268,12 @@ wuffs_private_impl__high_prec_dec__parse(wuffs_private_impl__high_prec_dec* h, 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 { @@ -288,14 +286,12 @@ wuffs_private_impl__high_prec_dec__parse(wuffs_private_impl__high_prec_dec* h, 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 @@ -408,14 +404,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 8369e792..8508d381 100644 --- a/internal/cgen/base/intconv-submodule.c +++ b/internal/cgen/base/intconv-submodule.c @@ -215,6 +215,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++; @@ -225,6 +226,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 7cd94c49..97a940ad 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -21668,14 +21668,12 @@ wuffs_private_impl__high_prec_dec__parse(wuffs_private_impl__high_prec_dec* h, 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 { @@ -21688,14 +21686,12 @@ wuffs_private_impl__high_prec_dec__parse(wuffs_private_impl__high_prec_dec* h, 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 @@ -21808,14 +21804,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 == '+') { @@ -23585,6 +23579,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++; @@ -23595,6 +23590,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/test/c/std/json.c b/test/c/std/json.c index fe41ce2a..7e52e951 100644 --- a/test/c/std/json.c +++ b/test/c/std/json.c @@ -1082,6 +1082,22 @@ test_wuffs_strconv_parse_number_f64_regular() { } } + // Input that ends right after a sign or an exponent marker must be rejected + // without reading past the end of the slice. Use DEFAULT_OPTIONS, as + // ALLOW_UNDERSCORES takes a different code path. + { + const char* strs[] = {"", "+", "-", "1e", "1E", "12.5e"}; + for (size_t i = 0; i < WUFFS_TESTLIB_ARRAY_SIZE(strs); i++) { + wuffs_base__result_f64 r = wuffs_base__parse_number_f64( + wuffs_base__make_slice_u8((uint8_t*)(void*)strs[i], strlen(strs[i])), + WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS); + if (r.status.repr != wuffs_base__error__bad_argument) { + RETURN_FAIL("\"%s\": have \"%s\", want \"%s\"", strs[i], + r.status.repr, wuffs_base__error__bad_argument); + } + } + } + return NULL; } @@ -1245,6 +1261,23 @@ test_wuffs_strconv_parse_number_u64() { } } + // A bare "0x" or "0d" prefix is invalid, even with + // ALLOW_MULTIPLE_LEADING_ZEROES. Pass an explicit length (not strlen) so + // that the byte just past the end of the slice is a digit, which must not be + // read. + { + const char* strs[] = {"0x7", "0X7", "0d7", "0D7"}; + for (size_t i = 0; i < WUFFS_TESTLIB_ARRAY_SIZE(strs); i++) { + wuffs_base__result_u64 r = wuffs_base__parse_number_u64( + wuffs_base__make_slice_u8((uint8_t*)(void*)strs[i], 2), + WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_MULTIPLE_LEADING_ZEROES); + if (r.status.repr != wuffs_base__error__bad_argument) { + RETURN_FAIL("\"%.2s\": have \"%s\", want \"%s\"", strs[i], + r.status.repr, wuffs_base__error__bad_argument); + } + } + } + // Test WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES. { for (int o = 0; o < 2; o++) {