linyiru opened a new issue, #12495:
URL: https://github.com/apache/arrow-rs/issues/12495

   ### Describe the bug
   
   `GenericColumnReader::skip_records` does not terminate when a data page of a 
repeated column declares more values than it has repetition levels. Once the 
level decoder is exhausted, `skip_rep_levels` returns `(0, 0)`, nothing in the 
loop body advances, and `while remaining_records != 0` spins at 100% CPU.
   
   `read_records` handles the same page with an error (`Insufficient repetition 
levels read from column`), added in #6232. The skip path did not get the same 
check.
   
   Through the Arrow reader this is reached by `with_offset`, 
`with_row_selection` and row filters on any list column. A single flipped byte 
is enough: a byte-flip fuzz of 9 small files (4,000 mutations each, read with 
`with_offset(1)`) hung 6 times.
   
   ### To Reproduce
   
   ```rust
   use std::sync::Arc;
   
   use bytes::Bytes;
   use parquet::basic::{Encoding, Repetition, Type as PhysicalType};
   use parquet::column::page::{Page, PageMetadata, PageReader};
   use parquet::column::reader::{get_column_reader, get_typed_column_reader};
   use parquet::data_type::Int32Type;
   use parquet::errors::Result;
   use parquet::schema::types::{ColumnDescriptor, ColumnPath, Type};
   
   struct OnePage(Option<Page>);
   
   impl Iterator for OnePage {
       type Item = Result<Page>;
       fn next(&mut self) -> Option<Self::Item> {
           self.get_next_page().transpose()
       }
   }
   
   impl PageReader for OnePage {
       fn get_next_page(&mut self) -> Result<Option<Page>> {
           Ok(self.0.take())
       }
       fn peek_next_page(&mut self) -> Result<Option<PageMetadata>> {
           Ok(self.0.as_ref().map(|_| PageMetadata {
               num_rows: None,
               num_levels: Some(4),
               is_dict: false,
           }))
       }
       fn skip_next_page(&mut self) -> Result<()> {
           self.0 = None;
           Ok(())
       }
   }
   
   fn main() {
       // list<int32> element column: max definition level 1, max repetition 
level 1
       let leaf = Type::primitive_type_builder("element", PhysicalType::INT32)
           .with_repetition(Repetition::REQUIRED)
           .build()
           .unwrap();
       let path = ColumnPath::new(vec!["list".to_string(), 
"element".to_string()]);
       let descr = Arc::new(ColumnDescriptor::new(Arc::new(leaf), 1, 1, path));
   
       // The page declares 4 values, but its repetition levels are one RLE run 
of 2.
       let page = || {
           let rep_levels = [2 << 1, 0];
           let def_levels = [4 << 1, 1];
           let mut buf = Vec::new();
           buf.extend_from_slice(&rep_levels);
           buf.extend_from_slice(&def_levels);
           buf.extend([10_i32, 20, 30, 40].iter().flat_map(|v| 
v.to_le_bytes()));
           Page::DataPageV2 {
               buf: Bytes::from(buf),
               num_values: 4,
               encoding: Encoding::PLAIN,
               num_nulls: 0,
               num_rows: 4,
               def_levels_byte_len: 2,
               rep_levels_byte_len: 2,
               is_compressed: false,
               statistics: None,
           }
       };
       let reader = || {
           let pages = Box::new(OnePage(Some(page())));
           
get_typed_column_reader::<Int32Type>(get_column_reader(descr.clone(), pages))
       };
   
       let (mut values, mut def, mut rep) = (vec![], vec![], vec![]);
       let read = reader().read_records(3, Some(&mut def), Some(&mut rep), &mut 
values);
       println!("read_records(3): {read:?}");
   
       println!("skip_records(3): ...");
       let skipped = reader().skip_records(3); // never returns
       println!("skip_records(3): {skipped:?}");
   }
   ```
   
   Output on 60.2.0 and on main (d470f2d54):
   
   ```
   read_records(3): Err(General("Insufficient repetition levels read from 
column"))
   skip_records(3): ...
   ```
   
   and the process has to be killed.
   
   ### Expected behavior
   
   `skip_records` returns the same error as `read_records`.
   
   ### Additional context
   
   - `parquet/src/column/reader.rs:312` (`skip_records`), compare the check at 
lines 248-253 in `read_records`.
   - Same class as #1997 and #6232 (#6228), which fixed the read path.
   - Per SECURITY.md this is a bug rather than a vulnerability (an infinite 
loop on malformed input).
   
   This report was prepared with AI assistance (Claude Code). The reproduction 
was run against 60.2.0 and main.
   
   ### Willingness to contribute
   
   I plan to contribute a fix for this bug independently
   


-- 
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]

Reply via email to