Skip to content

Commit 865a992

Browse files
committed
simplify over-subscribed check
1 parent 392088a commit 865a992

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

zlib-rs/src/inflate/inftrees.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,12 @@ pub(crate) fn inflate_table(
7777
let root = bits.clamp(min, max);
7878

7979
/* check for an over-subscribed or incomplete set of lengths */
80-
let mut left = 1i32;
81-
let mut len = 1;
82-
while len <= MAX_BITS {
83-
left <<= 1;
84-
left -= count[len] as i32;
85-
if left < 0 {
86-
// over-subscribed
87-
return InflateTable::InvalidCode;
88-
}
89-
len += 1;
80+
let mut left = 1u32;
81+
for &sym in &count[1..] {
82+
left = match (left << 1).checked_sub(u32::from(sym)) {
83+
None => return InflateTable::InvalidCode, // over-subscribed
84+
Some(v) => v,
85+
};
9086
}
9187

9288
if left > 0 && (matches!(codetype, CodeType::Codes) || max != 1) {

0 commit comments

Comments
 (0)