Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parquet: Verify 32-bit CRC checksum when decoding pages #6290

Merged
merged 16 commits into from
Sep 28, 2024
5 changes: 4 additions & 1 deletion parquet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ zstd-sys = { version = ">=2.0.0, <2.0.14", default-features = false }
all-features = true

[features]
default = ["arrow", "snap", "brotli", "flate2", "lz4", "zstd", "base64"]
default = ["arrow", "snap", "brotli", "flate2", "lz4", "zstd", "base64", "crc"]
xmakro marked this conversation as resolved.
Show resolved Hide resolved
# Enable lz4
lz4 = ["lz4_flex"]
# Enable arrow reader/writer APIs
Expand All @@ -117,6 +117,8 @@ object_store = ["dep:object_store", "async"]
zstd = ["dep:zstd", "zstd-sys"]
# Display memory in example/write_parquet.rs
sysinfo = ["dep:sysinfo"]
# Verify 32-bit CRC checksum when decoding parquet pages
crc = ["dep:crc32fast"]

[[example]]
name = "read_parquet"
Expand Down Expand Up @@ -215,3 +217,4 @@ harness = false

[lib]
bench = false

xmakro marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions parquet/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub enum ParquetError {
/// Returned when reading into arrow or writing from arrow.
ArrowError(String),
IndexOutOfBound(usize, usize),
Crc32Mismatch,
xmakro marked this conversation as resolved.
Show resolved Hide resolved
/// An external error variant
External(Box<dyn Error + Send + Sync>),
}
Expand All @@ -60,6 +61,9 @@ impl std::fmt::Display for ParquetError {
ParquetError::IndexOutOfBound(index, ref bound) => {
write!(fmt, "Index {index} out of bound: {bound}")
}
ParquetError::Crc32Mismatch => {
write!(fmt, "Page CRC checksum mismatch")
}
ParquetError::External(e) => write!(fmt, "External: {e}"),
}
}
Expand Down
9 changes: 9 additions & 0 deletions parquet/src/file/serialized_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,15 @@ pub(crate) fn decode_page(
physical_type: Type,
decompressor: Option<&mut Box<dyn Codec>>,
) -> Result<Page> {
// Verify the 32-bit CRC checksum of the page
#[cfg(feature = "crc")]
if let Some(expected_crc) = page_header.crc {
let crc = crc32fast::hash(&buffer);
if crc != expected_crc as u32 {
return Err(ParquetError::Crc32Mismatch);
}
}

// When processing data page v2, depending on enabled compression for the
// page, we should account for uncompressed data ('offset') of
// repetition and definition levels.
Expand Down