Skip to content

Commit 97898e8

Browse files
ngoldbaumsethmlarson
authored andcommitted
Unconditionally consume buffers to avoid stale data
1 parent 3fd4d20 commit 97898e8

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

‎src/brotlicffi/_api.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,10 +497,8 @@ def _decompress(self, data, output_buffer_limit):
497497
chunks.append(chunk)
498498
chunks_len += len(chunk)
499499

500-
# Save any unconsumed input for the next call.
501-
if available_in[0] > 0:
502-
remaining_input = ffi.buffer(next_in[0], available_in[0])[:]
503-
self._unconsumed_data = remaining_input
500+
# Save input for the next call
501+
self._unconsumed_data = ffi.buffer(next_in[0], available_in[0])[:]
504502

505503
# Check if we've reached the output limit.
506504
if (

‎test/test_simple_decompression.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,25 @@ def test_decompressobj_with_output_buffer_limit(
8989
assert final_result == uncompressed_data
9090

9191

92+
def test_high_expansion_prefix_without_output_buffer_limit():
93+
"""
94+
A fully-consumed mid-stream chunk must not leave stale unconsumed
95+
input behind (issue #225).
96+
"""
97+
uncompressed = b''.join(
98+
bytes([65 + (i % 26)]) * 4096 for i in range(16)
99+
)
100+
compressed = brotlicffi.compress(uncompressed, lgwin=12)
101+
assert len(compressed) > 64
102+
103+
o = brotlicffi.Decompressor()
104+
result = o.decompress(compressed[:64])
105+
assert not o._unconsumed_data
106+
assert o.can_accept_more_data()
107+
result += o.decompress(compressed[64:])
108+
assert result == uncompressed
109+
110+
92111
def test_drip_feed(simple_compressed_file):
93112
"""
94113
Sending in the data one byte at a time still works.

0 commit comments

Comments
 (0)