AVRO-4323: [Java] Bound DataFileStream block size against available input before allocating the block buffer - #3919
Open
iemejia wants to merge 1 commit into
Open
Conversation
…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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
When reading an Avro data (container) file,
DataFileStreamreads each block's declared size as alongand validated it only against theIntegerrange before allocating the blockbyte[]buffer (inDataFileStream.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 firsthasNext()/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
IOExceptionbefore allocating. The check is skipped when the remaining count is unknown (-1), so non-seekable streams are unaffected.How was this patch tested?
TestDataFileReader:oversizedBlockSizeIsRejectedBeforeAllocation— a crafted file whose block header declares a size nearInteger.MAX_VALUEwith no block bytes now fails fast instead of attempting a large allocation.validFileWithSingleRecordStillReads— negative control confirming a valid file still reads.avromodule test suite passes.JIRA