Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 86 additions & 5 deletions parquet/src/parquet_thrift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,12 @@ pub(crate) trait ThriftCompactInputProtocol<'a> {

/// Read an `i16`.
fn read_i16(&mut self) -> ThriftProtocolResult<i16> {
Ok(self.read_zig_zag()? as _)
Ok(i16::try_from(self.read_zig_zag()?)?)
}

/// Read an `i32`.
fn read_i32(&mut self) -> ThriftProtocolResult<i32> {
Ok(self.read_zig_zag()? as _)
Ok(i32::try_from(self.read_zig_zag()?)?)
}

/// Read an `i64`.
Expand All @@ -458,13 +458,18 @@ pub(crate) trait ThriftCompactInputProtocol<'a> {
fn read_double(&mut self) -> ThriftProtocolResult<f64>;

/// Skip a ULEB128 encoded varint.
#[inline(always)]
fn skip_vlq(&mut self) -> ThriftProtocolResult<()> {
loop {
for index in 0..10 {
let byte = self.read_byte()?;
if index == 9 && byte > 1 {
return Err(ThriftProtocolError::IntegerOverflow);
}
if byte & 0x80 == 0 {
return Ok(());
}
}
unreachable!()
}

/// Skip a thrift [binary].
Expand Down Expand Up @@ -905,11 +910,12 @@ impl<W: Write> ThriftCompactOutputProtocol<W> {

/// Used to indicate the start of a list of `element_type` elements.
pub(crate) fn write_list_begin(&mut self, element_type: ElementType, len: usize) -> Result<()> {
let len = i32::try_from(len)?;
if len < 15 {
self.write_byte(((len as u8) << 4) | element_type as u8)
} else {
self.write_byte(0xf0u8 | element_type as u8)?;
self.write_vlq(len as _)
self.write_vlq(len as u64)
}
}

Expand All @@ -922,7 +928,7 @@ impl<W: Write> ThriftCompactOutputProtocol<W> {
/// Serialize a slice of `u8`s. This will encode a length, and then write the bytes without
/// further encoding.
pub(crate) fn write_bytes(&mut self, val: &[u8]) -> Result<()> {
self.write_vlq(val.len() as u64)?;
self.write_vlq(i32::try_from(val.len())? as u64)?;
self.writer.write_all(val)?;
Ok(())
}
Expand Down Expand Up @@ -1298,6 +1304,47 @@ pub(crate) mod tests {
}
}

#[test]
fn test_skip_vlq_stops_at_tenth_byte() {
// `last` is the tenth varint byte, after nine continuation bytes.
// 0 and 1 terminate valid values; 2 exceeds the single remaining bit in u64;
// 0x80 requests an invalid eleventh byte. The following 42 must remain unread.
for last in [0, 1, 2, 0x80] {
let mut bytes = vec![0x80; 9];
bytes.extend_from_slice(&[last, 42]);
let mut slice = ThriftSliceInputProtocol::new(&bytes);
let mut remaining = bytes.as_slice();
let stream_result = ThriftReadInputProtocol::new(&mut remaining).skip_vlq();
let slice_result = slice.skip_vlq();
if last <= 1 {
slice_result.unwrap();
stream_result.unwrap();
} else {
assert!(matches!(
slice_result,
Err(ThriftProtocolError::IntegerOverflow)
));
assert!(matches!(
stream_result,
Err(ThriftProtocolError::IntegerOverflow)
));
}
// we only get here if we've properly skipped the preceding int64
assert_eq!(slice.as_slice(), &[42]);
assert_eq!(remaining, &[42]);
}

// also test truncated VLQ int (all continuation bits set)
let bytes = [0x80; 9];
let mut slice = ThriftSliceInputProtocol::new(&bytes);
assert!(matches!(slice.skip_vlq(), Err(ThriftProtocolError::Eof)));
let mut stream = ThriftReadInputProtocol::new(bytes.as_slice());
assert!(matches!(
stream.skip_vlq(),
Err(ThriftProtocolError::IO(e)) if e.kind() == ErrorKind::UnexpectedEof
));
}

#[test]
fn test_binary_length_bound() {
let oversized = [0x80, 0x80, 0x80, 0x80, 8]; // i32::MAX + 1
Expand Down Expand Up @@ -1448,6 +1495,40 @@ pub(crate) mod tests {
}
}

#[test]
fn test_unknown_integer_before_known_field() {
// `last` is the tenth varint byte, after nine continuation bytes.
// 0 terminates a padded zero; 2 exceeds the single remaining bit in u64;
// 0x80 sets the continuation bit, requesting an invalid eleventh byte.
for last in [0, 2, 0x80] {
// Unknown I64 field 1, then known field 2 containing i32(42).
// The decoder will call skip_vlq for field 1, and then read field 2.
// A malformed field 1 should now error.
let mut bytes = vec![0x16];
bytes.extend_from_slice(&[0x80; 9]);
bytes.extend_from_slice(&[last, 0x15, 84, 0]);
let mut slice = ThriftSliceInputProtocol::new(&bytes);
let mut stream = ThriftReadInputProtocol::new(bytes.as_slice());
if last == 0 {
assert_eq!(KnownField::read_thrift(&mut slice).unwrap().value, 42);
assert_eq!(KnownField::read_thrift(&mut stream).unwrap().value, 42);
} else {
assert!(
KnownField::read_thrift(&mut slice)
.unwrap_err()
.to_string()
.contains("integer overflow")
);
assert!(
KnownField::read_thrift(&mut stream)
.unwrap_err()
.to_string()
.contains("integer overflow")
);
}
}
}

#[test]
fn test_skip_boolean_collections_before_known_field() {
for collection_type in [FieldType::List, FieldType::Set] {
Expand Down