From 88242ffef3b00423572db66318becd5206880d94 Mon Sep 17 00:00:00 2001 From: Jan-Erik Rediger Date: Thu, 12 May 2016 16:24:48 +0200 Subject: [PATCH] fix: Avoid out-of-bounds indexing This was found by QuickCheck. --- src/compress.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/compress.rs b/src/compress.rs index a72ffdc..9ac7a23 100644 --- a/src/compress.rs +++ b/src/compress.rs @@ -140,7 +140,7 @@ pub fn compress(data: &[u8]) -> LzfResult> { current_offset += 1; } else { /* one more literal byte we must copy */ - if current_offset >= out_buf_len { + if out_len >= out_buf_len as i32 { return Err(LzfError::NoCompressionPossible); } @@ -252,3 +252,10 @@ fn test_alice_wonderland_both() { assert_eq!(&compressed[..], &c_compressed[..]); } + +#[test] +fn quickcheck_found_bug() { + let inp = vec![0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 1, 1, 0, 1, 2, 0, 1, 3, 0, 1, 4, 0, 0, 5, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 0, 9, 0, 0, 10, 0, 0, 11, 0, 1, 5, 0, 1, 6, 0, 1, 7, 0, 1, 8, 0, 1, 9, 0, 1, 10, 0, 0]; + + assert_eq!(LzfError::NoCompressionPossible, compress(&inp).unwrap_err()); +}