Skip to content

Commit ea5fbcc

Browse files
authored
zlib: return output inflate still holds after the last input byte (#8909)
* zlib: return output inflate still holds after the last input byte decompress_chunks returned before it called inflate when the input was empty, and returned once the input was consumed even when the output buffer was full. Raw deflate has no trailer, so inflate can consume the last input byte and still hold output. Decompress.decompress(b"", n) and Decompress.flush() then returned nothing, and zlib.decompress with a small bufsize raised "incomplete or truncated stream". Mirror CPython's zlibmodule.c: call inflate for empty input, and call it again while it fills the output buffer. A stream with no pending output answers Z_BUF_ERROR, so an empty input before any compressed data still leaves eof false. Assisted-by: Claude Code:claude-opus-5-5 Fixes #8907 * zlib: fold the full-buffer case into the loop's exit condition Return when the stream ended, or when the input is used up and inflate left part of the output buffer empty. A full buffer falls through to the existing next iteration, as CPython's loop runs while avail_out is zero, instead of a separate branch ahead of the exit check. Assisted-by: Claude Code:claude-opus-5-5 * zlib: drop the comment that stood in for the removed early return Assisted-by: Claude Code:claude-opus-5-5
1 parent 20295fd commit ea5fbcc

2 files changed

Lines changed: 97 additions & 4 deletions

File tree

‎crates/common/src/compression/zlib.rs‎

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,6 @@ fn decompress_chunks(
406406
max_length: Option<usize>,
407407
calc_flush: impl Fn(bool) -> InflateFlush,
408408
) -> Result<(Vec<u8>, bool), String> {
409-
if data.is_empty() {
410-
return Ok((Vec::new(), false));
411-
}
412409
let max_length = max_length.unwrap_or(usize::MAX);
413410
let mut buf = Vec::new();
414411

@@ -434,7 +431,9 @@ fn decompress_chunks(
434431
match res {
435432
Ok(status) => {
436433
let stream_end = status == Status::StreamEnd;
437-
if stream_end || data.is_empty() {
434+
// A full output buffer can leave output pending in inflate,
435+
// even with no input left: only a partial buffer ends it.
436+
if stream_end || (data.is_empty() && produced < additional) {
438437
buf.shrink_to_fit();
439438
return Ok((buf, stream_end));
440439
} else if !chunk.is_empty() && consumed == 0 {
@@ -1078,4 +1077,56 @@ mod tests {
10781077
assert_eq!(d.unconsumed_tail(), b"");
10791078
assert_eq!(d.unused_data(), b"NEXT");
10801079
}
1080+
1081+
/// Decode raw deflate of `b"x" * 1168` in 100-byte steps until all
1082+
/// input is consumed. Inflate then still holds the last 68 bytes.
1083+
fn drained_raw_deflate() -> (Decompressor, Vec<u8>) {
1084+
let mut co = Compressor::new(6, 8, -MAX_WBITS, 8, 0, None).unwrap();
1085+
let mut raw = co.compress(&[b'x'; 1168]).unwrap();
1086+
raw.extend(co.flush(Z_FINISH).unwrap());
1087+
let mut d = Decompressor::new(-MAX_WBITS, None).unwrap();
1088+
let mut out = d.decompress(&raw, Some(100)).unwrap();
1089+
while !d.unconsumed_tail().is_empty() {
1090+
let tail = d.unconsumed_tail().to_vec();
1091+
out.extend(d.decompress(&tail, Some(100)).unwrap());
1092+
}
1093+
assert_eq!(out.len(), 1100);
1094+
assert!(!d.eof());
1095+
(d, out)
1096+
}
1097+
1098+
#[test]
1099+
fn empty_input_returns_pending_output() {
1100+
let (mut d, mut out) = drained_raw_deflate();
1101+
out.extend(d.decompress(b"", Some(100)).unwrap());
1102+
assert_eq!(out, [b'x'; 1168]);
1103+
assert!(d.eof());
1104+
}
1105+
1106+
#[test]
1107+
fn flush_returns_pending_output() {
1108+
let (mut d, mut out) = drained_raw_deflate();
1109+
out.extend(d.flush(DEF_BUF_SIZE).unwrap());
1110+
assert_eq!(out, [b'x'; 1168]);
1111+
assert!(d.eof());
1112+
}
1113+
1114+
#[test]
1115+
fn small_bufsize_returns_pending_output() {
1116+
let data = [b'x'; 1168];
1117+
let raw = compress(&data, 6, -MAX_WBITS).unwrap();
1118+
for bufsize in [1, 100] {
1119+
assert_eq!(decompress(&raw, -MAX_WBITS, bufsize).unwrap(), data);
1120+
}
1121+
}
1122+
1123+
#[test]
1124+
fn empty_flush_does_not_finish_a_stream() {
1125+
let encoded = compress(b"later input", -1, MAX_WBITS).unwrap();
1126+
let mut d = Decompressor::new(MAX_WBITS, None).unwrap();
1127+
assert_eq!(d.flush(DEF_BUF_SIZE).unwrap(), b"");
1128+
assert!(!d.eof());
1129+
assert_eq!(d.decompress(&encoded, None).unwrap(), b"later input");
1130+
assert!(d.eof());
1131+
}
10811132
}

‎extra_tests/snippets/stdlib_zlib.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,45 @@
7979
assert d.flush() == b""
8080
assert d.unconsumed_tail == tail
8181
assert d.unused_data == b"NEXT" + tail
82+
83+
84+
# Raw deflate has no trailer, so inflate can consume the last input byte and
85+
# still hold output. decompress(b"", n) and flush() must return that output.
86+
def drained_raw_deflate():
87+
c = zlib.compressobj(6, zlib.DEFLATED, -15)
88+
raw = c.compress(b"x" * 1168) + c.flush()
89+
d = zlib.decompressobj(-15)
90+
out = d.decompress(raw, 100)
91+
while d.unconsumed_tail:
92+
out += d.decompress(d.unconsumed_tail, 100)
93+
assert len(out) == 1100
94+
assert not d.eof
95+
return d, out
96+
97+
98+
d, out = drained_raw_deflate()
99+
more = d.decompress(b"", 100)
100+
assert len(more) == 68
101+
assert d.eof
102+
assert d.flush() == b""
103+
assert out + more == b"x" * 1168
104+
105+
d, out = drained_raw_deflate()
106+
rest = d.flush()
107+
assert len(rest) == 68
108+
assert d.eof
109+
assert out + rest == b"x" * 1168
110+
111+
# An empty input before any compressed data does not end the stream.
112+
d = zlib.decompressobj()
113+
assert d.decompress(b"") == b""
114+
assert not d.eof
115+
assert d.flush() == b""
116+
assert not d.eof
117+
assert d.decompress(zlib.compress(b"later input")) == b"later input"
118+
assert d.eof
119+
120+
# The one-shot decompress drains the same pending output with a small bufsize.
121+
raw = zlib.compress(b"x" * 1168, 6, -15)
122+
for bufsize in (1, 100):
123+
assert zlib.decompress(raw, -15, bufsize) == b"x" * 1168

0 commit comments

Comments
 (0)