Skip to content

Bound the nesting depth of building and reading a GenericDatum - #39

Merged
PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:bound-generic-datum-depth
Oct 2, 2026
Merged

PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:bound-generic-datum-depth

Conversation

@groeneai

@groeneai groeneai commented Oct 1, 2026

Copy link
Copy Markdown

A named type may reference itself, and both GenericDatum construction and GenericReader::read follow the reference with no limit:

  • GenericDatum::init builds every record field (and the first branch of a union) eagerly, so A{b: B{a: A}} recursed forever in the GenericRecord constructor;
  • GenericReader::read recurses once per nesting level of the data, so A{next: ["null", "A"]} overflowed the stack on a value nested 1,000,000 levels deep.

maxSchemaDepth_ (#31) bounds only the textual nesting of the schema JSON, and a self-reference is one shallow lookup there.

Both now throw avro::Exception beyond GenericDatum::maxNestingDepth (256). The branch is based on ClickHouse's current pin (6640c55c752e), so bumping to it carries no other fork change.

Fixes the crash in ClickHouse/ClickHouse#123044, bumped in ClickHouse/ClickHouse#123084. Checked with ClickHouse built against this commit: the four crashing cases (the required cycle, and nullable, array and map cycles nested 1,000,000 levels deep) now fail with this exception instead of SIGSEGV, and 88 Avro/Iceberg stateless tests give the same results as before, except the new one.

A named type may reference itself, and both recursions follow the reference
without any limit:

- `GenericDatum::init` builds every field of a record eagerly (and the first
  branch of a union), so a schema whose cycle has no terminating branch, such
  as `A{b: B{a: A}}`, recursed forever in the `GenericRecord` constructor;
- `GenericReader::read` recurses once per nesting level of the data, so a
  terminating cycle such as `A{next: ["null", "A"]}` overflowed the stack on a
  value nested a million levels deep.

`maxSchemaDepth_` does not help: it bounds the textual nesting of the schema
JSON, and a self-reference is one shallow lookup there.

Both now throw an `Exception` beyond `GenericDatum::maxNestingDepth` (256, the
schema depth limit ClickHouse passes to `DataFileReaderBase`). ClickHouse
builds `GenericDatum` only for Iceberg manifest lists and its own manifest
schemas, which nest a few levels.

Reported in ClickHouse/ClickHouse#123044 (an Iceberg INSERT carrying forward a
crafted manifest list crashed the server).
@PedroTadim
PedroTadim merged commit 1d1ad4b into ClickHouse:master Oct 2, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 2, 2026
Moves the pin from the ClickHouse/avro#39 commit to the current master
(merge of ClickHouse/avro#36). Besides ClickHouse#39 (already pinned), this brings in
ClickHouse#34 (bound the declared object count by a schema-derived minimum record
size), ClickHouse#36 (memoise that walk, so it terminates on recursive schemas) and
ClickHouse#37 (test fix).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 5, 2026
Keep only the recursive record (fails building the datum) and the
1,000,000-level nullable cycle (fails reading it), and assert which of
the two ClickHouse/avro#39 depth checks fires, with its error code.
Drop the controls and the server liveness probe; the test uses only
`clickhouse local`.

The contrib/avro bump is no longer part of this PR: master already
pins the same commit since ClickHouse#121211.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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