iemejia commented on code in PR #3921:
URL: https://github.com/apache/avro/pull/3921#discussion_r3727555056
##########
lang/java/trevni/core/src/main/java/org/apache/trevni/ColumnValues.java:
##########
@@ -90,8 +90,12 @@ private void startBlock(int block) throws IOException {
this.row = column.firstRows[block];
in.seek(column.blockStarts[block]);
- int end = column.blocks[block].compressedSize;
- byte[] raw = new byte[end + checksum.size()];
+ // Validate the declared compressed size against the bytes remaining (after
+ // the seek) before allocating, and guard the addition of the checksum size
+ // against integer overflow, so a malformed, corrupted, or truncated file
+ // cannot drive an oversized or negative allocation.
+ int end = in.checkLength(column.blocks[block].compressedSize, 1);
+ byte[] raw = new byte[Math.addExact(end, checksum.size())];
in.readFully(raw);
Review Comment:
Good catch on both points. `startBlock` now validates the combined
`compressedSize + checksum.size()` length against the bytes remaining (computed
in `long` to avoid overflow) and rejects a negative or overflowing size with an
`IOException`, replacing `Math.addExact` (which could surface an unchecked
`ArithmeticException`). A file truncated before the checksum is therefore
rejected up front rather than reading past the remaining bytes. (d8ffcef)
##########
lang/java/trevni/core/src/test/java/org/apache/trevni/TestColumnFile.java:
##########
@@ -58,6 +61,33 @@ void emptyFile(ColumnFileMetaData fileMeta) throws Exception
{
in.close();
}
+ /** Byte offset of the little-endian 4-byte columnCount field in the header.
*/
+ private static final int COLUMN_COUNT_OFFSET = 12; // MAGIC(4) + rowCount
fixed64(8)
Review Comment:
Done — the offset is now derived as `ColumnFileWriter.MAGIC.length +
Long.BYTES` instead of the hard-coded `12`, so it stays correct if the magic or
fixed-width encoding sizes change. (d8ffcef)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]