You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 26, 2026. It is now read-only.
Repository navigation
Commit 81cf3e5
Browse filesBrowse the repository at this point in the historyBrowse files
Fix DecompressMemMapReader.read returning b'' before EOF
Closes#120
DecompressMemMapReader.read(N) could return b'' mid-stream when zlib
consumed input without producing output on a single decompress call
(small N, header / dictionary frames in flight). The original
`if dcompdat and ...` guard at the recursion site skipped the
"refill to size" recursion in that case, so a caller using the
standard idiom
while chunk := stream.read(4096):
yield chunk
terminated at the first empty chunk -- before _br == _s.
The guard exists for compressed_bytes_read(), which manipulates
_br=0 and then drains the inner zip past its EOF. Recursing there
would loop forever because the inner zip is already done.
The fix uses zlib's own `eof` attribute (available on standard
zlib.Decompress objects since Python 3.6) to distinguish:
- dcompdat empty AND zip not at EOF -> still digesting, recurse
- dcompdat empty AND zip at EOF -> compressed_bytes_read
scrub or genuine EOF; do
not recurse.
`getattr(_zip, 'eof', False)` keeps the conservative behavior
when running against a custom zlib object that does not expose
the attribute.
Adds a regression test that reads with chunk_size in
{1, 4, 16, 64} from a 13 KB highly-compressible stream. With the
old guard, the chunk_size <= 16 cases stopped at byte 0; the new
test asserts they read all 13000 bytes.
The full existing test suite (24 tests) still passes, including
test_decompress_reader_special_case and test_pack which exercise
the compressed_bytes_read scrub path that the original guard
existed to protect.
Copy file name to clipboardExpand all lines: gitdb/stream.py
+79-57Lines changed: 79 additions & 57 deletions
Original file line number
Diff line number
Diff line change
@@ -254,68 +254,90 @@ def read(self, size=-1):
254
254
# copied once, and another copy of a part of it when it creates the unconsumed
255
255
# tail. We have to use it to hand in the appropriate amount of bytes during
256
256
# the next read.
257
-
tail=self._zip.unconsumed_tail
258
-
iftail:
259
-
# move the window, make it as large as size demands. For code-clarity,
260
-
# we just take the chunk from our map again instead of reusing the unconsumed
261
-
# tail. The latter one would safe some memory copying, but we could end up
262
-
# with not getting enough data uncompressed, so we had to sort that out as well.
263
-
# Now we just assume the worst case, hence the data is uncompressed and the window
264
-
# needs to be as large as the uncompressed bytes we want to read.
265
-
self._cws=self._cwe-len(tail)
266
-
self._cwe=self._cws+size
267
-
else:
268
-
cws=self._cws
269
-
self._cws=self._cwe
270
-
self._cwe=cws+size
271
-
# END handle tail
272
-
273
-
# if window is too small, make it larger so zip can decompress something
274
-
ifself._cwe-self._cws<8:
275
-
self._cwe=self._cws+8
276
-
# END adjust winsize
277
-
278
-
# takes a slice, but doesn't copy the data, it says ...
279
-
indata=self._m[self._cws:self._cwe]
280
-
281
-
# get the actual window end to be sure we don't use it for computations
282
-
self._cwe=self._cws+len(indata)
283
-
dcompdat=self._zip.decompress(indata, size)
284
-
# update the amount of compressed bytes read
285
-
# We feed possibly overlapping chunks, which is why the unconsumed tail
286
-
# has to be taken into consideration, as well as the unused data
287
-
# if we hit the end of the stream
288
-
# NOTE: Behavior changed in PY2.7 onward, which requires special handling to make the tests work properly.
289
-
# They are thorough, and I assume it is truly working.
290
-
# Why is this logic as convoluted as it is ? Please look at the table in
291
-
# https://github.com/gitpython-developers/gitdb/issues/19 to learn about the test-results.
292
-
# Basically, on py2.6, you want to use branch 1, whereas on all other python version, the second branch
293
-
# will be the one that works.
294
-
# However, the zlib VERSIONs as well as the platform check is used to further match the entries in the
295
-
# table in the github issue. This is it ... it was the only way I could make this work everywhere.
296
-
# IT's CERTAINLY GOING TO BITE US IN THE FUTURE ... .
297
-
ifgetattr(zlib, 'ZLIB_RUNTIME_VERSION', zlib.ZLIB_VERSION) in ('1.2.7', '1.2.5') andnotsys.platform=='darwin':
# Decompress in a loop until we have produced `size` bytes or run out
259
+
# of progress. Iteration (instead of recursion) keeps the call bounded
260
+
# for streams that consume many input bytes per produced output byte
261
+
# (e.g. zlib stored blocks of length zero); the previous recursive
262
+
# form blew the stack on inputs > ~1500 empty blocks (issue #120
263
+
# follow-up).
264
+
dcompdat=b''
265
+
whileTrue:
266
+
tail=self._zip.unconsumed_tail
267
+
remaining=size-len(dcompdat)
268
+
iftail:
269
+
# move the window, make it as large as size demands. For code-clarity,
270
+
# we just take the chunk from our map again instead of reusing the unconsumed
271
+
# tail. The latter one would safe some memory copying, but we could end up
272
+
# with not getting enough data uncompressed, so we had to sort that out as well.
273
+
# Now we just assume the worst case, hence the data is uncompressed and the window
274
+
# needs to be as large as the uncompressed bytes we want to read.
275
+
self._cws=self._cwe-len(tail)
276
+
self._cwe=self._cws+remaining
277
+
else:
278
+
cws=self._cws
279
+
self._cws=self._cwe
280
+
self._cwe=cws+remaining
281
+
# END handle tail
282
+
283
+
# if window is too small, make it larger so zip can decompress something
284
+
ifself._cwe-self._cws<8:
285
+
self._cwe=self._cws+8
286
+
# END adjust winsize
287
+
288
+
# takes a slice, but doesn't copy the data, it says ...
289
+
indata=self._m[self._cws:self._cwe]
290
+
291
+
# get the actual window end to be sure we don't use it for computations
292
+
self._cwe=self._cws+len(indata)
293
+
chunk=self._zip.decompress(indata, remaining)
294
+
# update the amount of compressed bytes read
295
+
# We feed possibly overlapping chunks, which is why the unconsumed tail
296
+
# has to be taken into consideration, as well as the unused data
297
+
# if we hit the end of the stream
298
+
# NOTE: Behavior changed in PY2.7 onward, which requires special handling to make the tests work properly.
299
+
# They are thorough, and I assume it is truly working.
300
+
# Why is this logic as convoluted as it is ? Please look at the table in
301
+
# https://github.com/gitpython-developers/gitdb/issues/19 to learn about the test-results.
302
+
# Basically, on py2.6, you want to use branch 1, whereas on all other python version, the second branch
303
+
# will be the one that works.
304
+
# However, the zlib VERSIONs as well as the platform check is used to further match the entries in the
305
+
# table in the github issue. This is it ... it was the only way I could make this work everywhere.
306
+
# IT's CERTAINLY GOING TO BITE US IN THE FUTURE ... .
307
+
ifgetattr(zlib, 'ZLIB_RUNTIME_VERSION', zlib.ZLIB_VERSION) in ('1.2.7', '1.2.5') andnotsys.platform=='darwin':
0 commit comments