AVRO-4241: [Java] BinaryDecoder should verify available bytes before reading#3725
Open
iemejia wants to merge 1 commit intoapache:mainfrom
Open
Conversation
ef9c52a to
ce192d2
Compare
…reading Add ensureAvailableBytes() pre-check in readString, readBytes, readArrayStart, arrayNext, readMapStart, and mapNext to verify the source has sufficient data before proceeding. Byte-array-backed sources return an exact remaining count. Stream-backed sources return buffered bytes plus InputStream.available(), which is reliable for the finite streams used by DataFileReader and DataFileStream. Includes regression tests and updated array/map limit tests.
ce192d2 to
8471fc2
Compare
Contributor
|
To cherry pick to 1.12! |
There was a problem hiding this comment.
Pull request overview
This PR strengthens Java binary decoding against truncated/malicious inputs by adding “bytes remaining” awareness and using it to fail fast (EOF) before allocating large buffers or collection backing structures.
Changes:
- Add a
Decoder#remainingBytes()API (default-1) and plumb it throughBinaryDecoderandValidatingDecoder. - Add early “ensure available bytes” checks for
BinaryDecoder.readString/readBytesand schema-aware prechecks for array/map block counts inGenericDatumReader. - Add/adjust regression tests covering string/bytes length validation and array/map byte-limit behavior.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| lang/java/avro/src/main/java/org/apache/avro/io/Decoder.java | Introduces remainingBytes() default API for decoders. |
| lang/java/avro/src/main/java/org/apache/avro/io/BinaryDecoder.java | Implements remainingBytes() and adds early byte-availability checks for string/bytes reads. |
| lang/java/avro/src/main/java/org/apache/avro/io/ValidatingDecoder.java | Delegates remainingBytes() to the underlying decoder. |
| lang/java/avro/src/main/java/org/apache/avro/generic/GenericDatumReader.java | Adds schema-aware byte validation for arrays/maps before allocating/reading blocks. |
| lang/java/avro/src/main/java/org/apache/avro/util/ByteBufferInputStream.java | Implements available() to support accurate remaining-byte reporting. |
| lang/java/avro/src/test/java/org/apache/avro/io/TestBinaryDecoder.java | Adds tests ensuring EOF is thrown before large allocations for string/bytes. |
| lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericDatumReader.java | Adds unit tests for minBytesPerElement and end-to-end collection byte validation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| int buffered = ba.getLim() - ba.getPos(); | ||
| try { | ||
| if (in.getClass() == ByteArrayInputStream.class || in.getClass() == ByteBufferInputStream.class) { | ||
| return buffered + in.available(); |
Comment on lines
+408
to
+411
| int minBytesPerEntry = 1 + minBytesPerElement(valueSchema); | ||
| if (count > 0) { | ||
| int remaining = decoder.remainingBytes(); | ||
| if (remaining >= 0 && count * (long) minBytesPerEntry > remaining) { |
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.
Add ensureAvailableBytes() pre-check in readString, readBytes, readArrayStart, arrayNext, readMapStart, and mapNext to verify the source has sufficient data before proceeding.
Byte-array-backed sources return an exact remaining count. Stream-backed sources return buffered bytes plus InputStream.available(), which is reliable for the finite streams used by DataFileReader and DataFileStream.
Includes regression tests and updated array/map limit tests.
R: @RyanSkraba @martin-g
What is the purpose of the change
(For example: This pull request improves file read performance by buffering data, fixing AVRO-XXXX.)
Verifying this change
(Please pick one of the following options)
This change is a trivial rework / code cleanup without any test coverage.
(or)
This change is already covered by existing tests, such as (please describe tests).
(or)
This change added tests and can be verified as follows:
(example:)
Documentation