Skip to content

Treat 20-digit positive overflows as big integers - #2793

Merged
lemire merged 1 commit into
simdjson:masterfrom
Yusufihsangorgel:fix/bigint-positive-20-digit-overflow
Jul 28, 2026
Merged

lemire merged 1 commit into
simdjson:masterfrom
Yusufihsangorgel:fix/bigint-positive-20-digit-overflow

Conversation

@Yusufihsangorgel

Copy link
Copy Markdown
Contributor

In the digit_count == longest_digit_count block of parse_number, the negative arm returns BIGINT_NUMBER on overflow while the positive arm returns INVALID_NUMBER, so every integer in [2^64, 10^20) comes back as a malformed number. The digit_count > longest_digit_count check above it already returns BIGINT_NUMBER for both signs, which is why 21 digits worked and 20 did not.

doc/tape.md uses 99999999999999999999 as its worked example of a big integer stored on the string tape. That literal is inside the broken window, so the example does not currently work.

This changes the positive arm to BIGINT_NUMBER(src). The // Positive overflow check: comment above that branch already establishes that both cases it covers are overflow, so the only thing in question was which error to report.

parse_number is shared, so this also affects On Demand: get_number() on a positive 20-digit overflow returned NUMBER_ERROR while get_number_type() already reported big_integer. That is why one of the tests is in the On Demand suite.

Three tests in two files, each of which fails without the one-line change:

  • tests/dom/big_integer_tests.cpp - the window boundaries 2^64 and 10^20 - 1 parse to the expected digit strings under number_as_string(true); and default mode returns BIGINT_ERROR rather than NUMBER_ERROR.
  • tests/ondemand/ondemand_number_tests.cpp - positive_big_int, mirroring the existing negative_big_int next to it.

18446744073709551615 staying a uint64_t is already covered by normal_numbers_unaffected.

One behavior change: with number_as_string off, that range now yields BIGINT_ERROR instead of NUMBER_ERROR, matching the negative range and 21-digit numbers.

This has a second consequence. The bigint returns skip the is_not_structural_or_whitespace(*p) check that follows them, and the recovery in tape_builder rescans the digits without validating the next byte, so trailing garbage is accepted. That is pre-existing on the paths that already returned BIGINT_NUMBER, and this change extends it to the 20-digit window:

input, number_as_string(true) master with this change
{"v":18446744073709551616x} NUMBER_ERROR parses
{"v":100000000000000000000x} parses parses
{"v":-18446744073709551616x} parses parses

Tightening that means validating the tail on all three bigint paths, which is wider than this issue and would reject inputs that parse today. Doing it for only the arm I touched would make it stricter than its siblings. Happy to take it here or separately, whichever you prefer. I have not pinned the current behavior with a test, since I do not think it is the behavior you want.

I compiled and ran big_integer_tests, basictests, ondemand_number_tests, ondemand_wrong_type_error_tests and ondemand_parse_api_tests against this change; all pass. I left singleheader/ untouched.

As on the issue: investigation and drafting were AI-assisted. I have checked the change and reproduced every measurement above.

Fixes #2791

The digit_count == longest_digit_count block returned BIGINT_NUMBER for a
negative overflow but INVALID_NUMBER for the positive one, so every integer
in [2^64, 10^20) was reported as a malformed number. The check above it
already returns BIGINT_NUMBER for both signs, which is why 21 digits worked.

Fixes simdjson#2791
@lemire
lemire merged commit e3720ba into simdjson:master Jul 28, 2026
112 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

number_as_string never sees 20-digit positive overflows: they return NUMBER_ERROR, not BIGINT_ERROR

2 participants