Skip to content

Commit 5f09ae4

Browse files
committed
add a number of additional (debug) asserts to spot invalid states earlier
1 parent 034e5f0 commit 5f09ae4

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

Diff for: zlib-rs/src/deflate.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ pub fn init(stream: &mut z_stream, config: DeflateConfig) -> ReturnCode {
259259
};
260260

261261
let w_size = 1 << window_bits;
262+
assert!(w_size >= MIN_LOOKAHEAD);
262263
let window = Window::new_in(&alloc, window_bits);
263264

264265
let prev = alloc.allocate_slice_raw::<u16>(w_size);
@@ -1149,7 +1150,14 @@ impl<'a> BitWriter<'a> {
11491150

11501151
match u16::from_le_bytes([dist_low, dist_high]) {
11511152
0 => self.emit_lit(ltree, lc) as usize,
1152-
dist => self.emit_dist(ltree, dtree, lc, dist),
1153+
dist => {
1154+
assert!(
1155+
(dist >> 7) < 256,
1156+
"invalid dist value {dist} from bytes {:?}",
1157+
[dist_low, dist_high, lc]
1158+
);
1159+
self.emit_dist(ltree, dtree, lc, dist)
1160+
}
11531161
};
11541162
}
11551163

Diff for: zlib-rs/src/read_buf.rs

+5
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,18 @@ impl<'a> ReadBuf<'a> {
5353
#[inline(always)]
5454
pub fn push_lit(&mut self, byte: u8) {
5555
// NOTE: we rely on the buffer being zeroed here!
56+
assert_eq!(&self.buf.as_slice()[self.filled..][..3], &[0, 0, 0]);
57+
5658
self.buf.as_mut_slice()[self.filled + 2] = byte;
5759

5860
self.filled += 3;
5961
}
6062

6163
#[inline(always)]
6264
pub fn push_dist(&mut self, dist: u16, len: u8) {
65+
// we expect the buffer to be zeroed (though it does not matter for correctness)
66+
debug_assert_eq!(&self.buf.as_slice()[self.filled..][..3], &[0, 0, 0]);
67+
6368
let buf = &mut self.buf.as_mut_slice()[self.filled..][..3];
6469
let [dist1, dist2] = dist.to_le_bytes();
6570

0 commit comments

Comments
 (0)