Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 01cb47b

Browse files
committed
[Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
1 parent a6f68e1 commit 01cb47b

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

Lib/gzip.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class GzipFile:
5555
"""
5656

5757
myfileobj = None
58+
max_read_chunk = 10 * 1024 * 1024 # 10Mb
5859

5960
def __init__(self, filename=None, mode=None,
6061
compresslevel=9, fileobj=None):
@@ -215,14 +216,14 @@ def read(self, size=-1):
215216
try:
216217
while True:
217218
self._read(readsize)
218-
readsize = readsize * 2
219+
readsize = min(self.max_read_chunk, readsize * 2)
219220
except EOFError:
220221
size = self.extrasize
221222
else: # just get some more of it
222223
try:
223224
while size > self.extrasize:
224225
self._read(readsize)
225-
readsize = readsize * 2
226+
readsize = min(self.max_read_chunk, readsize * 2)
226227
except EOFError:
227228
if size > self.extrasize:
228229
size = self.extrasize

Lib/test/test_gzip.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,29 @@ def test_append(self):
5858
f = gzip.GzipFile(self.filename, 'rb') ; d = f.read() ; f.close()
5959
self.assertEqual(d, (data1*50) + (data2*15))
6060

61+
def test_many_append(self):
62+
# Bug #1074261 was triggered when reading a file that contained
63+
# many, many members. Create such a file and verify that reading it
64+
# works.
65+
f = gzip.open(self.filename, 'wb', 9)
66+
f.write('a')
67+
f.close()
68+
for i in range(0,200):
69+
f = gzip.open(self.filename, "ab", 9) # append
70+
f.write('a')
71+
f.close()
72+
73+
# Try reading the file
74+
zgfile = gzip.open(self.filename, "rb")
75+
contents = ""
76+
while 1:
77+
ztxt = zgfile.read(8192)
78+
contents += ztxt
79+
if not ztxt: break
80+
zgfile.close()
81+
self.assertEquals(contents, 'a'*201)
82+
83+
6184
def test_readline(self):
6285
self.test_write()
6386
# Try .readline() with varying line lengths

0 commit comments

Comments
 (0)