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

Add check for uncompressed data #723

Merged
merged 2 commits into from
Aug 27, 2024
Merged
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
63 changes: 31 additions & 32 deletions src/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -966,31 +966,7 @@ impl<'data> CompressedData<'data> {
match self.format {
CompressionFormat::None => Ok(Cow::Borrowed(self.data)),
#[cfg(feature = "compression")]
CompressionFormat::Zlib => {
use core::convert::TryInto;
let size = self
.uncompressed_size
.try_into()
.ok()
.read_error("Uncompressed data size is too large.")?;
let mut decompressed = Vec::new();
decompressed
.try_reserve_exact(size)
.ok()
.read_error("Uncompressed data allocation failed")?;
let mut decompress = flate2::Decompress::new(true);
decompress
.decompress_vec(
self.data,
&mut decompressed,
flate2::FlushDecompress::Finish,
)
.ok()
.read_error("Invalid zlib compressed data")?;
Ok(Cow::Owned(decompressed))
}
#[cfg(feature = "compression")]
CompressionFormat::Zstandard => {
CompressionFormat::Zlib | CompressionFormat::Zstandard => {
use core::convert::TryInto;
use std::io::Read;
let size = self
Expand All @@ -1003,13 +979,36 @@ impl<'data> CompressedData<'data> {
.try_reserve_exact(size)
.ok()
.read_error("Uncompressed data allocation failed")?;
let mut decoder = ruzstd::StreamingDecoder::new(self.data)
.ok()
.read_error("Invalid zstd compressed data")?;
decoder
.read_to_end(&mut decompressed)
.ok()
.read_error("Invalid zstd compressed data")?;

match self.format {
CompressionFormat::Zlib => {
let mut decompress = flate2::Decompress::new(true);
decompress
.decompress_vec(
self.data,
&mut decompressed,
flate2::FlushDecompress::Finish,
)
.ok()
.read_error("Invalid zlib compressed data")?;
}
CompressionFormat::Zstandard => {
let mut decoder = ruzstd::StreamingDecoder::new(self.data)
.ok()
.read_error("Invalid zstd compressed data")?;
decoder
.read_to_end(&mut decompressed)
.ok()
.read_error("Invalid zstd compressed data")?;
}
_ => unreachable!(),
}
if size != decompressed.len() {
return Err(Error(
"Uncompressed data size does not match compression header",
));
}

Ok(Cow::Owned(decompressed))
}
_ => Err(Error("Unsupported compressed data.")),
Expand Down
Loading