Skip to content

Do not allocate sizes read from the file before data backs them - #48

Open
motsc wants to merge 11 commits into
masterfrom
limit-sizes-from-file
Open

motsc wants to merge 11 commits into
masterfrom
limit-sizes-from-file

Conversation

@motsc

@motsc motsc commented Nov 6, 2026 •

Copy link
Copy Markdown

Allocations driven by malformed Avro files previously preceded validation. GenericReader::read resized Array and Map values to their declared counts, DataFileReaderBase passed unchecked snappy lengths to snappy::Uncompress, and values with no input bytes could expand without a schema-size bound.

This validates snappy before allocation, grows containers as data is decoded, limits zero-input expansion, and reads string, bytes and fixed values incrementally. Reading through GenericReader now constructs records one field at a time, including the root and resolved defaults, using ShallowTag. The call can be interrupted while JSON or resolving decoders are active.

Ordinary eager GenericDatum(schema) construction and its const default access are unchanged. Untrusted-data readers should use ShallowTag, as the Iceberg and Hudi metadata paths do.

Found by fuzzing. The legacy and resolving decoders pass their existing suites plus the allocation and compatibility regression cases.

`GenericReader::read` resized `Array` and `Map` to the block count declared in the file, and `DataFileReaderBase::readDataBlock` let `snappy::Uncompress` allocate the length prefix of the compressed data. Grow the containers with the decoded items, and validate the snappy buffer first.
An `Array` whose items encode to zero bytes (`null`, an empty record, a zero-length `fixed`) is not bounded by the input, so the block count declared in the file still drove unbounded growth. Cap the total item count of such arrays at 2^24 and throw `avro::Exception` beyond it.
motsc added a commit to ClickHouse/ClickHouse that referenced this pull request Nov 7, 2026
`contrib/avro` now caps arrays whose items encode to zero bytes (ClickHouse/avro#48).
motsc added 9 commits November 7, 2026 14:30
Weight the zero-byte budget by the number of GenericDatum objects an item
materializes (a record builds one per field), and charge it only when
decoding the item did not advance the input stream. Items that consume input
are bounded by the input and are no longer limited when resolving schemas.
…the input advances

The budget for values that take no bytes was shared by a whole top-level datum and only
charged array items that consumed no input, so it rejected valid input-bounded data
(`array<array<null>>` with 70000 items) and did not see default maps, strings or union
branches at all.

Array items, map entries, selected union branches and strings that are not backed by the
input are now charged when they are created, to a budget of 65536 datums that refills
whenever `Decoder::inputPosition` advances. A stretch of the input that does not advance
is bounded however arrays, maps, records and defaults are nested, and input that does
advance is bounded by its size.

`JsonParser::position` no longer counts the token that was peeked but not consumed, so
JSON arrays of scalars are not mistaken for items that take no bytes.
…cted

`GenericReader` charged the datums of a selected union branch after `selectBranch` had
already constructed them, and constructing a branch builds the datum of every field of a
record eagerly. A union `["null", R]` where `R` has 70000 fields therefore allocated the
whole record, a couple of MiB from a one-byte index, before the budget was consulted.

The count of the branch now comes from its schema, with `datumNodeCount`, and is charged
before `selectBranch` runs. Only the selected branch is counted, so a recursive branch is
not rejected for the branches that it could reach, and unions at the root, in records,
array items, map values and the unions that a resolving decoder selects are all covered.

`GenericDatum::branchSchema` returns the schema of a branch of a union datum without
constructing it, and reports an index outside the union as `Exception`.
GenericFixed resized its value to the size declared by the schema when constructed, and BinaryDecoder::decodeFixed resized to the declared size before reading, so a schema declaring a huge fixed allocated it before any input was read. The constructor now leaves the value empty, decodeFixed reads in bounded chunks, and a fixed that is filled by a default is charged to the datum budget like strings and bytes. GenericWriter rejects a fixed whose value size differs from its schema.
…backs it

A `GenericDatum` constructed from a schema constructed the datums of every field of every record of the schema, which is exponential in the size of a schema whose records refer to a named record several times, before any input was read. `ShallowTag` constructs a datum whose record has no fields, whose array and map are empty and whose union holds its shallow first branch. `GenericReader` constructs the root, the items, the entries, the union branches and the fields shallow and charges each to the budget just before it constructs it, so a value that the input backs is not rejected for the datums that it holds, and what a stretch of the input that does not advance constructs is bounded by the budget.

The constructors without the tag are unchanged and stay eager.

The default of a record or union field of a schema is built shallow, so compiling a schema constructs what its default holds and not the datums of the schema.
Use the default buffer data pointer without indexing its first element. Records made entirely of null fields have an empty binary default, so indexing element zero triggers a hardened vector assertion before resolving can read the default.
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.

1 participant