Skip to content

Fix crash in MsgPack schema inference for a map with a nil or empty key - #127281

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-msgpack-schema-inference-null-map-key
Oct 23, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-msgpack-schema-inference-null-map-key

Conversation

@groeneai

@groeneai groeneai commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed a crash in MsgPack schema inference when the first key of a map is nil, an empty array or an empty map. Such a column is now reported as impossible to infer, like a nil value, and its type can be given with schema_inference_hints.

Description

On 26.3 and later, schema inference over a MsgPack map whose first key is nil or an empty array/map (for example DESC format(MsgPack, '\x81\xc0\x01') SETTINGS input_format_msgpack_number_of_columns = 1) crashes the server with a null pointer dereference. Measured on the official 26.3.42.3 and 26.8.18.2 images and on master. Any file()/url()/s3()/format() inference over such data reaches it.

Found by the AST fuzzer in Stress test (amd_msan), STID 1719-3b0f: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=214443bbdffd8fee15e5a6ad62b3e07602a2b437&name_0=MasterCI&name_1=Stress%20test%20%28amd_msan%29. No related open issue found.

Root cause: MsgPackSchemaReader::getDataType returns nullptr for a value it cannot type. In the MAP case the key type went through removeNullable before the key_type && value_type check, and removeNullable dereferences its argument.

Change: removeNullable is applied only after the null check, so an untypeable key behaves like an untypeable value: the type comes from a later row, or inference fails with CANNOT_EXTRACT_TABLE_STRUCTURE (ONLY_NULLS_WHILE_READING_SCHEMA). Inputs that worked before infer the same type.

Validation: the new test 05441_msgpack_schema_inference_untypeable_map_key covers {nil: 1}, {[]: 1}, {{}: 1}, {{nil: 1}: 1}, inference from a later row and a schema_inference_hints override. It kills an unfixed server and passes 50/50 with randomized settings on the fixed one.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/127281]

Version info

  • Merged into: 26.11.1.613-master (included in 26.11 and later)
  • Backported to: 26.10.2.94, 26.9.26.7, 26.8.35.6, 26.7.38.13, 26.3.61.4

…rst key

`MsgPackSchemaReader::getDataType` returns `nullptr` when it cannot infer a
type from a value (nil, an empty array or map, or a container of such values).
In the MAP case the key type was passed to `removeNullable` before the
`key_type && value_type` check, so a map whose first key is nil, an empty
array or an empty map (for example the 3-byte input `\x81\xc0\x01`, `{nil: 1}`)
dereferenced a null type and crashed the server during schema inference.

Apply `removeNullable` only after the null check, so an untypeable key follows
the same "cannot infer" path as an untypeable value: the column type is taken
from a later row, or schema inference fails with ONLY_NULLS_WHILE_READING_SCHEMA
(wrapped in CANNOT_EXTRACT_TABLE_STRUCTURE) and can be overridden with
`schema_inference_hints`.

Found by the AST fuzzer in Stress test (amd_msan), STID 1719-3b0f.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review: my own cold review plus an independent model (engine: codex; 0 findings). 1 nit total.

# Sev Finding Verdict Evidence / action
1 💡 The four DESC lines assert CANNOT_EXTRACT_TABLE_STRUCTURE, the generic wrapper readSchemaFromFormat uses for any schema-reader error, so alone they would not catch a different inference error AGREE, noted, not blocking The nested error is ONLY_NULLS_WHILE_READING_SCHEMA in all 52 local runs. The "cannot infer" behaviour is pinned by the later-row and schema_inference_hints queries, and an unfixed server dies on every null-key line

Severity: ❌ blocker / ⚠️ major / 💡 nit.

Session id: cron:clickhouse-review-slot-8:20261021-212600

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. DESC format(MsgPack, '\x81\xc0\x01') SETTINGS input_format_msgpack_number_of_columns = 1 ({nil: 1}) segfaults every time; so do {[]: 1}, {{}: 1} and {{nil: 1}: 1}.
b Root cause explained? MsgPackSchemaReader::getDataType returns nullptr for an untypeable value. In the MAP case the key type was passed to removeNullable before the key_type && value_type check, and removeNullable dereferences it.
c Fix matches root cause? Yes. removeNullable moved behind the existing null check at the one call site; removeNullable itself is unchanged.
d Test intent preserved / new tests added? New test 05441_msgpack_schema_inference_untypeable_map_key: four null-key shapes, inference from a later row, a schema_inference_hints override, and a positive control.
e Both directions demonstrated? Unfixed binary: SIGSEGV in removeNullable <- getDataType for all four shapes, and the test kills the server. Fixed binary: CANNOT_EXTRACT_TABLE_STRUCTURE (nested ONLY_NULLS_WHILE_READING_SCHEMA), test OK, 50/50 with randomized settings.
f Fix is general across code paths? This is the only removeNullable(get...Type(...)) in src/Formats and src/Processors/Formats. The MAP value, the ARRAY path and the other schema readers check for null before use.
g Fix generalizes across inputs (params/datatypes/wrappers)? Covered: nil, empty array, empty map, nested map with a nil key, later-row inference, hint override. getDataType never returns Nullable/LowCardinality, so wrappers do not apply.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change; inputs that worked infer the same type.
i Invariants and contracts preserved? The schema-reader contract (nullptr means "cannot infer from this value") now also holds for map keys; IRowSchemaReader::readSchema handles it as before.

Session id: cron:clickhouse-impl-slot-4:20261021-205200

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @vitlibar @al13n321, could you review this and add the can be tested label so CI runs? In the MsgPack schema reader the MAP case passed the first key's type to removeNullable before the null check, so a map whose first key is nil or an empty array/map dereferenced a null type during schema inference; removeNullable now runs after the check.

@clickhouse-gh

clickhouse-gh Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a8fba07]

Summary: ✅


AI Review

Summary

PR #127281 fixes a null pointer dereference in MsgPack schema inference by checking inferred map-key and value types before calling removeNullable. The regression test covers untypeable keys, nested maps, later-row inference, and schema_inference_hints. No actionable findings. CI evidence on commit 95472dd6ca8a70f6a5cc82e35944d2efa7baa19e confirms 50/50 passing runs under both ASan/UBSan and MSan. Tests were not rerun locally.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). labels Oct 21, 2026
@Algunenano Algunenano added the can be tested Allows running workflows for external contributors label Oct 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing a8fba07ff with master e0ae5a4bb (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of d2ac903cf; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 10 s compile time in total, 7 of them have a recent master baseline.

Job report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 95472dd

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stateless tests (arm_binary, s3 storage, DBReplicated, parallel, 2/2) / 05407_distributed_plan_exchange_compression_default flaky, not from this PR (it changes only MsgPack schema inference): 05407_default and 05407_empty_lz4 print 1 0 instead of 1 1 under the randomized group_by_two_level_threshold_bytes = 1 with use_statistics = 0 (runner: 50/50 fail with these settings, 55/55 pass without); it failed the same way on master and on 30 other PRs in 3 days. The fix merged after this run had started #127186 (mine, merged)
Mergeable Check, PR praktika rollups of the row above #127186 (the owner of the row above)

The two Config Workflow / Pre Hooks and Finish Workflow / Post Hooks failures on this head are the earlier attempts stopped at the can be tested label; the current attempt passed, including both Bugfix validation (functional tests, *) jobs. CIDB has no other FAIL or ERROR row on this head.

Session id: cron:our-pr-ci-monitor:20261022-023003

@PedroTadim PedroTadim self-assigned this Oct 22, 2026
@alexey-milovidov alexey-milovidov added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Oct 23, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 23, 2026
Merged via the queue into ClickHouse:master with commit 3d44c5b Oct 23, 2026
179 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 23, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 23, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 23, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 23, 2026
Backport #127281 to 26.10: Fix crash in MsgPack schema inference for a map with a nil or empty key
clickhouse-gh Bot added a commit that referenced this pull request Oct 23, 2026
Backport #127281 to 26.9: Fix crash in MsgPack schema inference for a map with a nil or empty key
clickhouse-gh Bot added a commit that referenced this pull request Oct 23, 2026
Backport #127281 to 26.8: Fix crash in MsgPack schema inference for a map with a nil or empty key
clickhouse-gh Bot pushed a commit that referenced this pull request Oct 23, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 23, 2026
Backport #127281 to 26.7: Fix crash in MsgPack schema inference for a map with a nil or empty key
alexey-milovidov added a commit that referenced this pull request Oct 23, 2026
Backport #127281 to 26.3: Fix crash in MsgPack schema inference for a map with a nil or empty key
leshikus pushed a commit that referenced this pull request Nov 13, 2026
VighneshPath pushed a commit to VighneshPath/ClickHouse that referenced this pull request Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants