Skip to content

Do not drop empty or duplicate keys in fractured_json table rows - #2908

Merged
lemire merged 1 commit into
simdjson:masterfrom
mvanslobbe:fix/fractured-table-keys
Oct 5, 2026
Merged

lemire merged 1 commit into
simdjson:masterfrom
mvanslobbe:fix/fractured-table-keys

Conversation

@mvanslobbe

Copy link
Copy Markdown
Contributor

Summary

fractured_json loses object keys when it lays out an array of objects
as a table. With default options:

simdjson::fractured_json_string(R"([[{"":1}]])");        // [ [ {1} ] ]
simdjson::fractured_json_string(R"([[{"a":1,"a":2}]])"); // [ [ {"a": 1} ] ]

The first output is not valid JSON. The second silently drops "a":2.
Both reproduce on v5.0.0 to v5.0.2 and on master.

Changes

  • Empty key. table_column used an empty key to mean "array row",
    so a column for the valid key "" was written without its key and
    colon. Add an explicit has_key flag and use it in the three places
    that tested !key.empty().
  • Duplicate keys. A table column holds one value per row, so later
    duplicates of a key were dropped. Duplicate names are valid JSON
    (RFC 8259 only says they SHOULD be unique), and the DOM, minify() and
    prettify() keep them. build_table_columns() now returns no columns
    when a row has a duplicate key, so the array is not laid out as a table
    (or as aligned compact rows) and the regular formatter writes every
    member. Duplicates are detected by recording the last row that filled
    each column, so there is no extra hashing or per-row allocation.
  • Add table_format_empty_key_test and table_format_duplicate_key_test
    (including an escaped duplicate, "a"). Each checks the exact
    output and that re-parsing it gives the same content as the input.

Arrays without empty or duplicate keys are formatted exactly as before.

Verification

  • The two new tests fail on master and pass with the fix.
  • fractured_json_tests: PASS with clang 22.1.8 (ASan + UBSan,
    -Wall -Wextra -Werror) and GCC 15.2.0 (-Wall -Wextra -Wpedantic -Werror).
  • Full CTest suite with SIMDJSON_SANITIZE=ON and
    SIMDJSON_DEVELOPER_MODE=ON (clang 22.1.8, Linux x86_64): 148/148 PASS.
  • git diff --check: PASS
  • Performance: fractured_json() on 200k rows such as
    {"id":1,"name":"n","x":1.5,"ok":true,"tags":[1,2]} (clang -O3, one
    pinned core, best of 7) takes 215-232 ms on both master and this branch.

Type of change

  • Bug fix
  • Documentation / tests

AI Assistance

I found these bugs with a libFuzzer harness that formats each parsed
document with random fractured_json_options, re-parses the output and
compares it with the input. Claude (an AI assistant) helped write the
harness, analyse the failures, draft the fix and tests, and review the
patch. I reviewed the final change and verified it with the steps above.
After the fix, the harness ran about 5.5M inputs without a failure.

When fractured_json lays out an array of objects as a table, it can lose
keys. With default options:

    fractured_json_string(R"([[{"":1}]])")         // [ [ {1} ] ]
    fractured_json_string(R"([[{"a":1,"a":2}]])")  // [ [ {"a": 1} ] ]

The first output is not valid JSON. The table code used an empty
table_column::key to mean "array row", so a column for the valid key ""
was written without its key and colon. Mark object columns with an
explicit has_key flag instead.

The second output silently loses "a":2. A table column holds one value
per row, so later duplicates of a key had nowhere to go. Duplicate names
are valid JSON (RFC 8259 only says they SHOULD be unique), and the DOM,
minify() and prettify() keep them. When a row has a duplicate key, do
not lay out the array as a table or as aligned compact rows, so the
regular formatter writes every member. Duplicates are detected by
recording the last row that filled each column, which adds no hashing
or per-row allocation.

Add regression tests for both cases.
@lemire

lemire commented Oct 5, 2026

Copy link
Copy Markdown
Member

Running tests.

@lemire
lemire merged commit 7fa77b1 into simdjson:master Oct 5, 2026
100 checks passed
@mvanslobbe
mvanslobbe deleted the fix/fractured-table-keys branch October 6, 2026 03:43
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.

2 participants