Skip to content

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

Closed

Description

@Yusufihsangorgel

With number_as_string(true), integers too large for uint64 come back as raw digit strings on the tape — except for one window, where the parse fails instead.

#include <cstdio>
#include "simdjson.h"
using namespace simdjson;

int main() {
  dom::parser parser;
  parser.number_as_string(true);
  dom::element doc;

  auto show = [&](const char* label, error_code e) {
    printf("%-20s -> %s\n", label, e ? error_message(e) : "ok");
  };

  show("21 digits",     parser.parse(R"({"v": 100000000000000000000})"_padded).get(doc));
  show("2^64",          parser.parse(R"({"v": 18446744073709551616})"_padded).get(doc));
  show("10^20 - 1",     parser.parse(R"({"v": 99999999999999999999})"_padded).get(doc));
  show("-2^64",         parser.parse(R"({"v": -18446744073709551616})"_padded).get(doc));
}
21 digits            -> ok
2^64                 -> NUMBER_ERROR: Problem while parsing a number
10^20 - 1            -> NUMBER_ERROR: Problem while parsing a number
-2^64                -> ok

So the whole range [2^64, 10^20) (every 20-digit positive integer above uint64 max) is rejected as a malformed number, while both its negative counterpart and anything one digit longer are handled.

Where it comes from

In numberparsing.h, the digit_count == longest_digit_count block treats the two signs differently:

if (negative) {
  // Anything negative above INT64_MAX+1 is invalid
  if (i > uint64_t(INT64_MAX)+1) { return BIGINT_NUMBER(src);  }
  ...
}  else if (src[0] != uint8_t('1') || i <= uint64_t(INT64_MAX)) { return INVALID_NUMBER(src); }

The negative arm returns BIGINT_NUMBER for an overflow; the positive arm returns INVALID_NUMBER for the same condition. The digit_count > longest_digit_count check above it returns BIGINT_NUMBER for both signs, which is why 21 digits works.

These inputs are well-formed JSON; the RFC 8259 number grammar is unbounded. They are also exactly the case the feature exists for: #2139 added BIGINT_ERROR to distinguish big integers from invalid numbers, #2640 added the DOM recovery path on top of it, and doc/dom.md says integers exceeding 64 bits are stored as raw digit strings on the tape.

What makes it look like an oversight rather than a decision is the #2139 diff. Before it, all three overflow returns in this function were INVALID_NUMBER. It converted two of them — the digit_count > longest_digit_count check and the negative arm — and left the positive arm as it was:

-  if (digit_count > longest_digit_count) { return INVALID_NUMBER(src); }
+  if (digit_count > longest_digit_count) { return BIGINT_NUMBER(src); }
...
-      if (i > uint64_t(INT64_MAX)+1) { return INVALID_NUMBER(src);  }
+      if (i > uint64_t(INT64_MAX)+1) { return BIGINT_NUMBER(src);  }

The positive arm does not appear in that diff at all. The long // Positive overflow check: comment above it, which I elided from the quote, explains how the overflow is detected; it predates the bigint work and says nothing about which error to return.

Returning BIGINT_NUMBER(src) from the positive arm on overflow would line the two up. I have not opened a PR in case there is a reason the arms differ that I am not seeing; if not, happy to send one with a test.

Found while making a Dart binding match dart:convert, which widens all of these to a double. Reproduced on 4.6.4 and on master as of today.

Investigation and drafting were AI-assisted. The repro compiles and prints the output above on both 4.6.4 and current master, and every code reference here was checked against the file it cites.

Activity

  1. lemire commented on Jul 25, 2026

    @lemire
    Member

    @Yusufihsangorgel Please open a PR, ideally with tests.

  2. added a commit that references this issue on Jul 28, 2026
    e3720ba
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions