Skip to content

Commit

Permalink
It's not an error to pass a buffer larger than the decompressed data.
Browse files Browse the repository at this point in the history
The reference implementation specifies that the size of the destination
buffer is presumed to be an upper bound. Passing too large a buffer is
not an error. Overwriting the buffer is handled as an error internally.
  • Loading branch information
TannerRogalsky committed Dec 31, 2021
1 parent d3e3dab commit 93fdf9c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 41 deletions.
14 changes: 2 additions & 12 deletions src/block/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,7 @@ pub fn decompress(input: &[u8], uncompressed_size: usize) -> Result<Vec<u8>, Dec
let mut vec: Vec<u8> = Vec::with_capacity(uncompressed_size);
let decomp_len =
decompress_internal::<_, false>(input, &mut VecSink::new(&mut vec, 0, 0), b"")?;
if decomp_len != uncompressed_size {
return Err(DecompressError::UncompressedSizeDiffers {
expected: uncompressed_size,
actual: decomp_len,
});
}
vec.truncate(decomp_len);
Ok(vec)
}

Expand All @@ -481,12 +476,7 @@ pub fn decompress_with_dict(
let mut vec: Vec<u8> = Vec::with_capacity(uncompressed_size);
let decomp_len =
decompress_internal::<_, true>(input, &mut VecSink::new(&mut vec, 0, 0), ext_dict)?;
if decomp_len != uncompressed_size {
return Err(DecompressError::UncompressedSizeDiffers {
expected: uncompressed_size,
actual: decomp_len,
});
}
vec.truncate(decomp_len);
Ok(vec)
}

Expand Down
26 changes: 8 additions & 18 deletions src/block/decompress_safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,17 +324,12 @@ pub fn decompress_size_prepended(input: &[u8]) -> Result<Vec<u8>, DecompressErro

/// Decompress all bytes of `input` into a new vec.
#[inline]
pub fn decompress(input: &[u8], uncompressed_size: usize) -> Result<Vec<u8>, DecompressError> {
let mut decompressed: Vec<u8> = Vec::with_capacity(uncompressed_size);
decompressed.resize(uncompressed_size, 0);
pub fn decompress(input: &[u8], max_uncompressed_size: usize) -> Result<Vec<u8>, DecompressError> {
let mut decompressed: Vec<u8> = Vec::with_capacity(max_uncompressed_size);
decompressed.resize(max_uncompressed_size, 0);
let decomp_len =
decompress_internal::<_, false>(input, &mut SliceSink::new(&mut decompressed, 0), b"")?;
if decomp_len != uncompressed_size {
return Err(DecompressError::UncompressedSizeDiffers {
expected: uncompressed_size,
actual: decomp_len,
});
}
decompressed.truncate(decomp_len);
Ok(decompressed)
}

Expand All @@ -353,19 +348,14 @@ pub fn decompress_size_prepended_with_dict(
#[inline]
pub fn decompress_with_dict(
input: &[u8],
uncompressed_size: usize,
max_uncompressed_size: usize,
ext_dict: &[u8],
) -> Result<Vec<u8>, DecompressError> {
let mut decompressed: Vec<u8> = Vec::with_capacity(uncompressed_size);
decompressed.resize(uncompressed_size, 0);
let mut decompressed: Vec<u8> = Vec::with_capacity(max_uncompressed_size);
decompressed.resize(max_uncompressed_size, 0);
let decomp_len =
decompress_internal::<_, true>(input, &mut SliceSink::new(&mut decompressed, 0), ext_dict)?;
if decomp_len != uncompressed_size {
return Err(DecompressError::UncompressedSizeDiffers {
expected: uncompressed_size,
actual: decomp_len,
});
}
decompressed.truncate(decomp_len);
Ok(decompressed)
}

Expand Down
11 changes: 0 additions & 11 deletions src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ pub enum DecompressError {
expected: usize,
actual: usize,
},
UncompressedSizeDiffers {
expected: usize,
actual: usize,
},
/// Literal is out of bounds of the input
LiteralOutOfBounds,
/// Expected another byte, but none found.
Expand Down Expand Up @@ -122,13 +118,6 @@ impl fmt::Display for DecompressError {
DecompressError::OffsetOutOfBounds => {
f.write_str("the offset to copy is not contained in the decompressed buffer")
}
DecompressError::UncompressedSizeDiffers { actual, expected } => {
write!(
f,
"the expected decompressed size differs, actual {}, expected {}",
actual, expected
)
}
}
}
}
Expand Down

0 comments on commit 93fdf9c

Please sign in to comment.