Skip to content

Commit c265c97

Browse files
committed
Fixed critical bug that could cause single bytes not to be returned in reads that should read all
1 parent 39b0042 commit c265c97

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

stream.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,7 @@ def read(self, size=-1):
249249

250250
# get the actual window end to be sure we don't use it for computations
251251
self._cwe = self._cws + len(indata)
252-
253252
dcompdat = self._zip.decompress(indata, size)
254-
255253
# update the amount of compressed bytes read
256254
# We feed possibly overlapping chunks, which is why the unconsumed tail
257255
# has to be taken into consideration, as well as the unused data
@@ -269,7 +267,7 @@ def read(self, size=-1):
269267
# Note: dcompdat can be empty even though we still appear to have bytes
270268
# to read, if we are called by compressed_bytes_read - it manipulates
271269
# us to empty the stream
272-
if dcompdat and len(dcompdat) < size and self._br < self._s:
270+
if dcompdat and (len(dcompdat) - len(dat)) < size and self._br < self._s:
273271
dcompdat += self.read(size-len(dcompdat))
274272
# END handle special case
275273
return dcompdat
Binary file not shown.

test/test_stream.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
DummyStream,
55
Sha1Writer,
66
make_bytes,
7-
make_object
7+
make_object,
8+
fixture_path
89
)
910

1011
from gitdb import *
1112
from gitdb.util import (
12-
NULL_HEX_SHA
13+
NULL_HEX_SHA,
14+
hex_to_bin
1315
)
1416

1517
from gitdb.util import zlib
@@ -135,4 +137,11 @@ def test_compressed_writer(self):
135137
os.remove(path)
136138
# END for each os
137139

138-
140+
def test_decompress_reader_special_case(self):
141+
odb = LooseObjectDB(fixture_path('objects'))
142+
ostream = odb.stream(hex_to_bin('7bb839852ed5e3a069966281bb08d50012fb309b'))
143+
144+
# if there is a bug, we will be missing one byte exactly !
145+
data = ostream.read()
146+
assert len(data) == ostream.size
147+

util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def unpack_from(fmt, data, offset=0):
5656
mkdir = os.mkdir
5757
chmod = os.chmod
5858
isdir = os.path.isdir
59+
isfile = os.path.isfile
5960
rename = os.rename
6061
dirname = os.path.dirname
6162
basename = os.path.basename
@@ -288,7 +289,7 @@ def _end_writing(self, successful=True):
288289
if self._write and successful:
289290
# on windows, rename does not silently overwrite the existing one
290291
if sys.platform == "win32":
291-
if os.path.isfile(self._filepath):
292+
if isfile(self._filepath):
292293
os.remove(self._filepath)
293294
# END remove if exists
294295
# END win32 special handling

0 commit comments

Comments
 (0)