diff --git a/parquet/src/parquet_thrift.rs b/parquet/src/parquet_thrift.rs index f8006028862d..735d6f452c60 100644 --- a/parquet/src/parquet_thrift.rs +++ b/parquet/src/parquet_thrift.rs @@ -441,12 +441,12 @@ pub(crate) trait ThriftCompactInputProtocol<'a> { /// Read an `i16`. fn read_i16(&mut self) -> ThriftProtocolResult { - Ok(self.read_zig_zag()? as _) + Ok(i16::try_from(self.read_zig_zag()?)?) } /// Read an `i32`. fn read_i32(&mut self) -> ThriftProtocolResult { - Ok(self.read_zig_zag()? as _) + Ok(i32::try_from(self.read_zig_zag()?)?) } /// Read an `i64`. @@ -458,13 +458,18 @@ pub(crate) trait ThriftCompactInputProtocol<'a> { fn read_double(&mut self) -> ThriftProtocolResult; /// 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]. @@ -905,11 +910,12 @@ impl ThriftCompactOutputProtocol { /// 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) } } @@ -922,7 +928,7 @@ impl ThriftCompactOutputProtocol { /// 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(()) } @@ -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 @@ -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] {