Skip to content

AVRO-4323: [Java] Bound DataFileStream block size against available input before allocating the block buffer - #3919

Open
iemejia wants to merge 1 commit into
apache:mainfrom
iemejia:AVRO-4323-datafilestream-block-size
Open

AVRO-4323: [Java] Bound DataFileStream block size against available input before allocating the block buffer#3919
iemejia wants to merge 1 commit into
apache:mainfrom
iemejia:AVRO-4323-datafilestream-block-size

Conversation

@iemejia

@iemejia iemejia commented Aug 5, 2026

Copy link
Copy Markdown
Member

What changes were proposed in this pull request?

When reading an Avro data (container) file, DataFileStream reads each block's declared size as a long and validated it only against the Integer range before allocating the block byte[] buffer (in DataFileStream.DataBlock). For a malformed, corrupted, or truncated file, the declared block size can be much larger than the number of bytes actually present, so the reader eagerly allocated a very large buffer on the first hasNext()/next() call before any block byte had been read.

This adds a check: when the number of bytes remaining in the input is known (byte-array- or known-length-stream-backed decoders), a declared block size larger than the bytes remaining is rejected with a clear IOException before allocating. The check is skipped when the remaining count is unknown (-1), so non-seekable streams are unaffected.

How was this patch tested?

  • New tests in TestDataFileReader:
    • oversizedBlockSizeIsRejectedBeforeAllocation — a crafted file whose block header declares a size near Integer.MAX_VALUE with no block bytes now fails fast instead of attempting a large allocation.
    • validFileWithSingleRecordStillReads — negative control confirming a valid file still reads.
  • Full avro module test suite passes.

JIRA

…nput

When reading a data (container) file, DataFileStream validated the declared
block size only against the Integer range before allocating the block buffer.
For a malformed, corrupted, or truncated file the declared size can greatly
exceed the bytes actually present, so the reader eagerly allocated a large
buffer before reading any block byte.

Reject a declared block size that exceeds the number of bytes remaining in the
input when that count is known (byte-array- or known-length-stream-backed
decoders), so reading a malformed file fails fast with a clear IOException.
The check is skipped when the remaining count is unknown (-1).
@github-actions github-actions Bot added the Java Pull Requests for Java binding label Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Java Pull Requests for Java binding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant