This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch 
gh-readonly-queue/main/pr-12215-1fc135b5fb84f2a54985c956e719f4318046dea8
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git

commit 4454c7db5a78effea05a13a16e7f8385eff59c30
Author: Phat Le <[email protected]>
AuthorDate: Fri Nov 13 03:33:58 2026 +0000

    fix(parquet): check PLAIN byte array end offsets for overflow in read 
(#12215)
    
    # Which issue does this PR close?
    
    - Closes #12200
    
    # Rationale for this change
    
    `ByteViewArrayDecoderPlain::read_impl` adds the page's `u32` length
    prefix to `start_offset` without an overflow check. On a 32-bit target
    (`wasm32`, `i686`) a corrupt length such as `0xFFFF_FFFC` wraps
    `end_offset` to a value below `start_offset`, the `end_offset > buf_len`
    check passes, and `buf.get_unchecked(start_offset..end_offset)` is
    called with an inverted range. #12179 fixed the same pattern in `skip`;
    `read` was left out.
    
    `ByteArrayDecoderPlain::read` has the same unchecked addition. It uses a
    checked slice, so there it is a panic rather than UB, but it is the same
    bug on the same input, so I fixed both.
    
    # What changes are included in this PR?
    
    Both `read` paths compute the end offset with
    `checked_add(..).filter(|&end| end <= buf.len())` and return the
    existing `EOF` error, the same form #12179 used for `skip`.
    
    # Are these changes tested?
    
    Yes, one unit test per decoder reads a page whose first length is
    `0xFFFF_FFFC` and expects an `EOF` error.
    
    On 64-bit the addition cannot wrap, so the tests pass there with and
    without the fix. I ran them on `i686-unknown-linux-gnu` (`cargo test -p
    parquet --lib --no-default-features --features arrow --target
    i686-unknown-linux-gnu`):
    
    - without the fix, debug: both tests panic with an add overflow at the
    changed lines;
    - without the fix, release: the offset decoder panics on the slice, and
    the view decoder returns `Ok` from the inverted range instead of an
    error;
    - with the fix: both pass.
    
    I did not run Miri (not available in my toolchain); CI runs it.
    
    # Are there any significant user-facing changes?
    
    No. A corrupt page that could panic or hit undefined behaviour on 32-bit
    targets now returns an error.
    
    ---
    
    AI disclosure: the patch and tests were drafted with AI assistance
    (Claude); I reviewed every line and ran fmt, clippy and the parquet
    array reader tests, plus the i686 runs above.
---
 parquet/src/arrow/array_reader/byte_array.rs      | 22 ++++++++++++++++----
 parquet/src/arrow/array_reader/byte_view_array.rs | 25 ++++++++++++++++++-----
 2 files changed, 38 insertions(+), 9 deletions(-)

diff --git a/parquet/src/arrow/array_reader/byte_array.rs 
b/parquet/src/arrow/array_reader/byte_array.rs
index 65c29e6d5a..07f1593f5c 100644
--- a/parquet/src/arrow/array_reader/byte_array.rs
+++ b/parquet/src/arrow/array_reader/byte_array.rs
@@ -390,10 +390,10 @@ impl ByteArrayDecoderPlain {
             let len = u32::from_le_bytes(len_bytes);
 
             let start_offset = self.offset + 4;
-            let end_offset = start_offset + len as usize;
-            if end_offset > buf.len() {
-                return Err(ParquetError::EOF("eof decoding byte 
array".into()));
-            }
+            let end_offset = start_offset
+                .checked_add(len as usize)
+                .filter(|&end| end <= buf.len())
+                .ok_or_else(|| ParquetError::EOF("eof decoding byte 
array".into()))?;
 
             output.try_push(&buf[start_offset..end_offset], 
self.validate_utf8)?;
 
@@ -769,6 +769,20 @@ mod tests {
         assert_eq!(decoder.max_remaining_values, 1);
     }
 
+    #[test]
+    fn test_plain_decoder_read_length_overflow_returns_error() {
+        // A length of 0xFFFF_FFFC after a 4 byte prefix ends at offset 2^32,
+        // which is 0 in a 32-bit usize.
+        let buffer = Bytes::from_static(&[0xFC, 0xFF, 0xFF, 0xFF, b'a', b'b', 
b'c', b'd']);
+        let mut decoder = ByteArrayDecoderPlain::new(buffer, 1, Some(1), 
false);
+        let mut output = OffsetBuffer::<i32>::with_capacity(0);
+
+        let error = decoder.read(&mut output, 1).unwrap_err();
+        assert!(matches!(error, ParquetError::EOF(_)), "{error}");
+        assert_eq!(decoder.offset, 0);
+        assert!(output.values.is_empty());
+    }
+
     #[test]
     fn test_plain_decoder_skip_past_end_returns_error() {
         let buffer = Bytes::from_static(&[0xFF, 0xFF, 0, 0, b'a']);
diff --git a/parquet/src/arrow/array_reader/byte_view_array.rs 
b/parquet/src/arrow/array_reader/byte_view_array.rs
index a9e436163f..38c27e7928 100644
--- a/parquet/src/arrow/array_reader/byte_view_array.rs
+++ b/parquet/src/arrow/array_reader/byte_view_array.rs
@@ -364,11 +364,10 @@ impl ByteViewArrayDecoderPlain {
                     .unwrap(),
             );
 
-            end_offset = start_offset + len as usize;
-
-            if end_offset > buf_len {
-                return Err(ParquetError::EOF("eof decoding byte 
array".into()));
-            }
+            end_offset = start_offset
+                .checked_add(len as usize)
+                .filter(|&end| end <= buf_len)
+                .ok_or_else(|| ParquetError::EOF("eof decoding byte 
array".into()))?;
 
             if VALIDATE_UTF8 {
                 // It seems you are trying to understand what's going on here, 
take a breath and be patient.
@@ -835,6 +834,22 @@ mod tests {
         assert_eq!(view_buffer.buffers.len(), 1);
     }
 
+    #[test]
+    fn test_byte_view_array_plain_decoder_read_length_overflow_returns_error() 
{
+        // A length of 0xFFFF_FFFC after a 4 byte prefix ends at offset 2^32,
+        // which is 0 in a 32-bit usize.
+        let buffer = Bytes::from_static(&[0xFC, 0xFF, 0xFF, 0xFF, b'a', b'b', 
b'c', b'd']);
+        for validate_utf8 in [false, true] {
+            let mut decoder =
+                ByteViewArrayDecoderPlain::new(buffer.clone(), 1, Some(1), 
validate_utf8);
+            let mut output = ViewBuffer::with_capacity(0);
+
+            let error = decoder.read(&mut output, 1).unwrap_err();
+            assert!(matches!(error, ParquetError::EOF(_)), "{error}");
+            assert!(output.views.is_empty());
+        }
+    }
+
     #[test]
     fn test_byte_view_array_plain_decoder_skip_past_end_returns_error() {
         let buffer = Bytes::from_static(&[0xFF, 0xFF, 0, 0, b'a']);

Reply via email to