Skip to content

Commit

Permalink
Fix buffer overflow bug
Browse files Browse the repository at this point in the history
See: #21
  • Loading branch information
sile committed Feb 19, 2019
1 parent 84c4350 commit a14390c
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/lz77/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ impl Lz77Encode for DefaultLz77Encoder {
backward_distance: distance as u16,
});
for k in (i..).take(length as usize).skip(1) {
if k >= end {
break;
}
prefix_table.insert(prefix(&self.buf[k..]), k as u32);
}
i += length as usize;
Expand Down Expand Up @@ -179,3 +182,28 @@ impl LargePrefixTable {
None
}
}

#[cfg(test)]
mod tests {
use super::*;
use deflate::symbol::Symbol;

#[test]
// See: https://github.com/sile/libflate/issues/21
fn issue21() {
let mut enc = DefaultLz77Encoder::new();
let mut sink = Vec::new();
enc.encode(b"aaaaa", &mut sink);
enc.flush(&mut sink);
assert_eq!(
sink,
vec![
Symbol::Literal(97),
Symbol::Share {
length: 4,
distance: 1
}
]
);
}
}

0 comments on commit a14390c

Please sign in to comment.