Skip to content

GH-3984: Reject negative lengths in DeltaLengthByteArrayValuesReader - #3985

Open
gitRasheed wants to merge 1 commit into
apache:masterfrom
gitRasheed:gh-3984-delta-length-negative
Open

gitRasheed wants to merge 1 commit into
apache:masterfrom
gitRasheed:gh-3984-delta-length-negative

Conversation

@gitRasheed

Copy link
Copy Markdown

Rationale for this change

DeltaLengthByteArrayValuesReader currently passes decoded lengths directly to ByteBufferInputStream without checking for negative values:

  • In readBytes(), SingleBufferInputStream.slice(length) only checks buffer.remaining() < length. A negative length returns an empty buffer and rewinds the stream position, causing subsequent values to re-read earlier bytes (for example, lengths [2, -2, 2] over abcd silently decode to ["ab", "", "ab"]). On a MultiBufferInputStream, a negative length silently returns an empty buffer.
  • In skip(int n), summing lengths in an int lets a negative length cancel out against positive lengths, lets a negative total rewind SingleBufferInputStream, and lets large valid lengths overflow an int to a negative value.
  • DeltaByteArrayReader delegates suffix reading to DeltaLengthByteArrayValuesReader, so DELTA_BYTE_ARRAY pages hit the same bug.

What changes are included in this PR?

  • Add readLength() in DeltaLengthByteArrayValuesReader to throw ParquetDecodingException when a decoded length is negative, and use it in both readBytes() and skip(int n).
  • Accumulate the byte count in skip(int n) as a long so summing non-negative int lengths cannot overflow.
  • Add unit tests in TestDeltaLengthByteArray and TestDeltaByteArray covering readBytes(), skip(n) with a negative length inside a positive sum, skip(n) with lengths overflowing int, and DELTA_BYTE_ARRAY negative suffix lengths.

This aligns with Arrow C++ (decoder.cc) and Spark's vectorized reader (VectorizedDeltaLengthByteArrayReader). Valid Parquet files only contain non-negative lengths, so valid data is unaffected.

Are these changes tested?

Yes. Added four unit tests:

  • TestDeltaLengthByteArray.testNegativeLength
  • TestDeltaLengthByteArray.testSkipNegativeLength
  • TestDeltaLengthByteArray.testSkipLengthsOverflowingInt
  • TestDeltaByteArray.testNegativeSuffixLength

All four fail on master (no exception thrown) and pass with the fix. Ran parquet-column tests, Spotless, and Apache RAT locally. Also verified end-to-end with crafted Parquet files read through ParquetReader, AvroParquetReader, and ParquetFileReader (v1/v2 pages, uncompressed and Snappy, DELTA_LENGTH_BYTE_ARRAY and DELTA_BYTE_ARRAY), which return ["ab", "", "ab"] without the fix and throw ParquetDecodingException with it.

Are there any user-facing changes?

Yes. Reading a corrupt DELTA_LENGTH_BYTE_ARRAY or DELTA_BYTE_ARRAY page with a negative value length now throws ParquetDecodingException instead of silently returning corrupted values. No public API changes.

Closes #3984

…eader

DeltaLengthByteArrayValuesReader passed decoded lengths directly to
ByteBufferInputStream without validating that they are non-negative.
On a SingleBufferInputStream, a negative length returned an empty slice
and rewound the stream position so subsequent values re-read earlier
bytes without error. skip(int n) had the same issue on negative sums,
missed negative lengths inside a positive sum, and could overflow an int
sum into a negative value. DeltaByteArrayReader was also affected via
its suffix reader.

Validate each decoded length in readLength() and throw
ParquetDecodingException when negative, and accumulate the skip total in
a long.
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.

DeltaLengthByteArrayValuesReader silently returns wrong values on negative lengths

1 participant