linyiru commented on issue #10734:
URL: https://github.com/apache/arrow-rs/issues/10734#issuecomment-7250660997
A related observation for Snappy pages, in case it is useful for the work on
this issue.
`SnappyCodec::decompress` resizes the output to the declared size,
decompresses into it, and leaves the buffer at the declared length
(`parquet/src/compression.rs:242-250`). Two consequences:
1. The size check in `decode_page` compares `decompressed.len()` with
`uncompressed_page_size`. For Snappy that is always true, so a header that
overstates the size is accepted and the page is padded with zeros. The other
codecs produce the real length and fail the check.
2. The declared size is allocated and zero-filled before any input is looked
at. An 11,789-byte file whose first page header says `uncompressed_page_size =
2^31 - 1` peaked at 2,055 MiB RSS here before failing on the next page.
```rust
// parquet = { version = "60.2.0", features = ["experimental"] }
use parquet::basic::Compression;
use parquet::compression::{create_codec, CodecOptions};
fn main() {
let mut codec = create_codec(Compression::SNAPPY,
&CodecOptions::default())
.unwrap()
.unwrap();
let mut compressed = Vec::new();
codec.compress(&[7; 100], &mut compressed).unwrap();
for declared in [100, 101, 1 << 30] {
let mut out = Vec::new();
let result = codec.decompress(&compressed, &mut out, Some(declared));
println!("declared {declared}: {result:?}, output buffer is {}
bytes", out.len());
}
}
```
```
declared 100: Ok(100), output buffer is 100 bytes
declared 101: Ok(100), output buffer is 101 bytes
declared 1073741824: Ok(100), output buffer is 1073741824 bytes
```
A Snappy stream starts with its own uncompressed length, so the codec can
compare the two before it resizes:
<details>
<summary>Diff</summary>
```diff
diff --git a/parquet/src/compression.rs b/parquet/src/compression.rs
index f3226da9a..ef5f45501 100644
--- a/parquet/src/compression.rs
+++ b/parquet/src/compression.rs
@@ -214,7 +214,7 @@ mod snappy_codec {
use snap::raw::{Decoder, Encoder, decompress_len, max_compress_len};
use crate::compression::Codec;
- use crate::errors::Result;
+ use crate::errors::{ParquetError, Result};
/// Codec for Snappy compression format.
pub struct SnappyCodec {
@@ -239,10 +239,14 @@ mod snappy_codec {
output_buf: &mut Vec<u8>,
uncompress_size: Option<usize>,
) -> Result<usize> {
- let len = match uncompress_size {
- Some(size) => size,
- None => decompress_len(input_buf)?,
- };
+ let len = decompress_len(input_buf)?;
+ if let Some(size) = uncompress_size
+ && size != len
+ {
+ return Err(general_err!(
+ "Snappy data declares an uncompressed size of {len}
bytes, expected {size}"
+ ));
+ }
let offset = output_buf.len();
output_buf.resize(offset + len, 0);
self.decoder
```
</details>
This does not bound memory in general, since a page can lie in both places.
It removes the silent zero padding and the cheapest form of the allocation, and
it is independent of the chunk-level bound discussed above. I can open a PR for
it with a test if that helps, or leave it for whoever picks up this issue.
This comment was prepared with AI assistance (Claude Code). The reproduction
was run against 60.2.0 and main.
--
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]