I spent a lot of time trying to reproduce this bug using the standard zlib decompressor without any success:
import gzip
import io
class EvilDecomp:
def __init__(self, **kwargs):
self.needs_input = False
self.unused_data = b""
self.eof = False
def decompress(self, data, max_length):
self.needs_input = True
return b""
gzip.zlib._ZlibDecompressor = EvilDecomp
with gzip.GzipFile(
fileobj=io.BytesIO(gzip.compress(b"hello world")), mode="rb"
) as f:
f.read(1)
22:16:40.678647000PM CEST maurycy@gimel /Users/maurycy/src/cpython % ./python.exe meow.py
Traceback (most recent call last):
File "/Users/maurycy/src/cpython/meow.py", line 18, in <module>
f.read(1)
~~~~~~^^^
File "/Users/maurycy/src/cpython/Lib/gzip.py", line 349, in read
return self._buffer.read(size)
~~~~~~~~~~~~~~~~~^^^^^^
File "/Users/maurycy/src/cpython/Lib/compression/_common/_streams.py", line 68, in readinto
data = self.read(len(byte_view))
File "/Users/maurycy/src/cpython/Lib/gzip.py", line 589, in read
if buf == b"":
^^^
UnboundLocalError: cannot access local variable 'buf' where it is not associated with a value
Bug report
Bug description:
This one is easy to spot but hard to reproduce:
cpython/Lib/gzip.py
Line 589 in d7dbde8
buf is initialized only when self._decompressor.needs_input:
cpython/Lib/gzip.py
Line 577 in d7dbde8
It exists since gh-95534 (#97664).
I spent a lot of time trying to reproduce this bug using the standard zlib decompressor without any success:
import gzip import io class EvilDecomp: def __init__(self, **kwargs): self.needs_input = False self.unused_data = b"" self.eof = False def decompress(self, data, max_length): self.needs_input = True return b"" gzip.zlib._ZlibDecompressor = EvilDecomp with gzip.GzipFile( fileobj=io.BytesIO(gzip.compress(b"hello world")), mode="rb" ) as f: f.read(1)results in:
22:16:40.678647000PM CEST maurycy@gimel /Users/maurycy/src/cpython % ./python.exe meow.py Traceback (most recent call last): File "/Users/maurycy/src/cpython/meow.py", line 18, in <module> f.read(1) ~~~~~~^^^ File "/Users/maurycy/src/cpython/Lib/gzip.py", line 349, in read return self._buffer.read(size) ~~~~~~~~~~~~~~~~~^^^^^^ File "/Users/maurycy/src/cpython/Lib/compression/_common/_streams.py", line 68, in readinto data = self.read(len(byte_view)) File "/Users/maurycy/src/cpython/Lib/gzip.py", line 589, in read if buf == b"": ^^^ UnboundLocalError: cannot access local variable 'buf' where it is not associated with a valueCPython versions tested on:
CPython main branch
Operating systems tested on:
macOS
Linked PRs