Skip to content

Reject a data file block header declaring a negative count - #33

Merged
PedroTadim merged 1 commit into
masterfrom
fix-badcount
Aug 25, 2026
Merged

PedroTadim merged 1 commit into
masterfrom
fix-badcount

Conversation

@PedroTadim

Copy link
Copy Markdown
Member

DataFileReaderBase::readDataBlock zigzag-decodes the block's object count and byte count straight off the wire and checked neither. A corrupted header can make both negative, and each fails in its own way: hasMore() tested the object count with != 0 while decr() only decrements, so a negative count never reached zero and the reader reported rows without end; a negative byte count wrapped in the cast to size_t into an effectively unbounded payload limit, letting the block read run past its own end.

The spec has both as non-negative, so reject them where they are read. hasMore() now tests > 0 and throws on a negative count, so a caller that decrements without asking fails there instead of looping.

Reported as a non-terminating SELECT count() over a corrupted Avro file in ClickHouse/ClickHouse#115437; the same block header is parsed for Iceberg manifest deserialization.

Make sure you have checked all steps below.

Jira

Tests

  • My PR adds the following unit tests OR does not need testing for this extremely good reason:

Commits

  • My commits all reference Jira issues in their subject lines. In addition, my commits follow the guidelines from "How to write a good git commit message":
    1. Subject is separated from body by a blank line
    2. Subject is limited to 50 characters (not including Jira issue reference)
    3. Subject does not end with a period
    4. Subject uses the imperative mood ("add", not "adding")
    5. Body wraps at 72 characters
    6. Body explains "what" and "why", not "how"

Documentation

  • In case of new functionality, my PR adds documentation that describes how to use it.
    • All the public functions and the classes in the PR contain Javadoc that explain what it does

DataFileReaderBase::readDataBlock zigzag-decodes the block's object count and
byte count straight off the wire and checked neither. A corrupted header can
make both negative, and each fails in its own way: hasMore() tested the object
count with `!= 0` while decr() only decrements, so a negative count never
reached zero and the reader reported rows without end; a negative byte count
wrapped in the cast to size_t into an effectively unbounded payload limit,
letting the block read run past its own end.

The spec has both as non-negative, so reject them where they are read.
hasMore() now tests `> 0` and throws on a negative count, so a caller that
decrements without asking fails there instead of looping.

Reported as a non-terminating `SELECT count()` over a corrupted Avro file in
ClickHouse/ClickHouse#115437; the same block header
is parsed for Iceberg manifest deserialization.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@PedroTadim

Copy link
Copy Markdown
Member Author

@Avogar @scanhex12 very easy to review

@PedroTadim

Copy link
Copy Markdown
Member Author

Going to merge, feel free to revert later on

@PedroTadim
PedroTadim merged commit f2d65e1 into master Aug 25, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Aug 25, 2026
ClickHouse/avro#33 rejects a data file block header declaring a negative
object or byte count, and hardens hasMore() to test > 0 instead of != 0.
That removes the non-termination on a negative declared count at the layer
that owns the header, so termination no longer depends on the caller asking
before it decrements.

The test asserts the error class instead of one message, because the
rejection can now come either from the library at the block header or from
the read path when the payload runs out. It gains a negative declared byte
count case, which the existing fixtures could not reach because the block
helper hardcoded a byte count of zero.

A block with a corrupted declared count is also a block that runs out of
input, so the class assertion alone cannot tell the header check from
payload exhaustion. Both negative counts therefore also assert the header
diagnostic by name, on both settings.

Measured on this tree, the same test file on three binaries:
  - with the bump and no count shortcut: passes.
  - without the bump: 12 lines differ, including all four header
    diagnostics and the negative object count reaching the query deadline.
  - with the bump and the count shortcut restored: 2 lines differ, the
    oversized declared count still answering 1000005000 from the header.
@PedroTadim
PedroTadim deleted the fix-badcount branch September 21, 2026 12:47
PedroTadim added a commit that referenced this pull request Sep 21, 2026
The test declared a six-long record and then wrote a single uint32_t
through it, so the header promised at least six bytes per record over a
four-byte payload. Since #34 the reader cross-checks a block's declared
object count against the payload it carries, and rejects that file:

  Object count 1 in block header cannot fit in 4 payload byte(s)
  for this schema

Rejecting it is the check working - the file is internally inconsistent
- so fix the test rather than the check. Write a conforming record
through the Pair/GenericRecord idiom the rest of the file already uses.
All six doc assertions, which are what the test is for, are unchanged.

Bisected by running this suite in isolation: it passes at 7b7f6a5 and
at f2d65e1 (#33) and fails from 57985a1 (#34) on. Isolation was needed
because boost aborts the run at the first fatal error, and an unrelated
test0/testWrite segfault on those older commits - fixed since - hides
this one. DataFileTests is now 69/69.

Co-Authored-By: Claude Opus 5 (1M context) <[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.

1 participant