Skip to content

Commit

Permalink
truncate decompressed buffer in block::decompress (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmarks authored Sep 26, 2024
1 parent 85a909f commit 0794b4a
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ pub fn decompress(src: &[u8], uncompressed_size: Option<i32>) -> Result<Vec<u8>>

let mut buffer = vec![0u8; size];

decompress_to_buffer(src, uncompressed_size, &mut buffer)?;
let sz = decompress_to_buffer(src, uncompressed_size, &mut buffer)?;
buffer.truncate(sz);
Ok(buffer)
}

Expand Down Expand Up @@ -294,6 +295,18 @@ mod test {

use super::compress_to_buffer;

/// This test will fail unless the buffer created by decompress is correctly truncated
#[test]
fn decompress_truncate_test() {
let src = "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111".as_bytes();
let rs_compressed = compress(src, None, false).unwrap();
let rs_compressed_rs_uncompressed =
decompress(&rs_compressed, Some((src.len() as i32) * 256)).unwrap();

// compare the uncompressed result from rust
assert_eq!(rs_compressed_rs_uncompressed, src,);
}

#[test]
fn test_compression_without_prefix() {
let size = 65536;
Expand Down

0 comments on commit 0794b4a

Please sign in to comment.