cpython: e406b8bd7b38 (original) (raw)
--- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -996,6 +996,20 @@ class OtherTests(unittest.TestCase): chk = zipfile.is_zipfile(fp) self.assertTrue(not chk)
- def test_damaged_zipfile(self):
"""Check that zipfiles with missing bytes at the end raise BadZipFile."""[](#l1.8)
# - Create a valid zip file[](#l1.9)
fp = io.BytesIO()[](#l1.10)
with zipfile.ZipFile(fp, mode="w") as zipf:[](#l1.11)
zipf.writestr("foo.txt", b"O, for a Muse of Fire!")[](#l1.12)
zipfiledata = fp.getvalue()[](#l1.13)
# - Now create copies of it missing the last N bytes and make sure[](#l1.15)
# a BadZipFile exception is raised when we try to open it[](#l1.16)
for N in range(len(zipfiledata)):[](#l1.17)
fp = io.BytesIO(zipfiledata[:N])[](#l1.18)
self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, fp)[](#l1.19)
+ def test_is_zip_valid_file(self): """Check that is_zipfile() correctly identifies zip files.""" # - passing a filename
--- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -196,6 +196,8 @@ def _EndRecData64(fpin, offset, endrec): return endrec data = fpin.read(sizeEndCentDir64Locator)
- if len(data) != sizeEndCentDir64Locator:
sig, diskno, reloff, disks = struct.unpack(structEndArchive64Locator, data) if sig != stringEndArchive64Locator: return endrecreturn endrec[](#l2.8)
@@ -206,6 +208,8 @@ def _EndRecData64(fpin, offset, endrec): # Assume no 'zip64 extensible data' fpin.seek(offset - sizeEndCentDir64Locator - sizeEndCentDir64, 2) data = fpin.read(sizeEndCentDir64)
- if len(data) != sizeEndCentDir64:
sig, sz, create_version, read_version, disk_num, disk_dir, [](#l2.18) dircount, dircount2, dirsize, diroffset = [](#l2.19) struct.unpack(structEndArchive64, data)return endrec[](#l2.17)
@@ -241,7 +245,9 @@ def _EndRecData(fpin): except OSError: return None data = fpin.read()
- if (len(data) == sizeEndCentDir and
data[0:4] == stringEndArchive and[](#l2.27)
data[-2:] == b"\000\000"):[](#l2.28) # the signature is correct and there's no comment, unpack structure[](#l2.29) endrec = struct.unpack(structEndArchive, data)[](#l2.30) endrec=list(endrec)[](#l2.31)
@@ -265,6 +271,9 @@ def _EndRecData(fpin): if start >= 0: # found the magic number; attempt to unpack and interpret recData = data[start:start+sizeEndCentDir]
if len(recData) != sizeEndCentDir:[](#l2.36)
# Zip file is corrupted.[](#l2.37)
return None[](#l2.38) endrec = list(struct.unpack(structEndArchive, recData))[](#l2.39) commentSize = endrec[_ECD_COMMENT_SIZE] #as claimed by the zip file[](#l2.40) comment = data[start+sizeEndCentDir:start+sizeEndCentDir+commentSize][](#l2.41)
@@ -276,7 +285,7 @@ def _EndRecData(fpin): endrec) # Unable to find a valid end of central directory structure
class ZipInfo (object): @@ -978,9 +987,11 @@ class ZipFile: total = 0 while total < size_cd: centdir = fp.read(sizeCentralDir)
if centdir[0:4] != stringCentralDir:[](#l2.55)
if len(centdir) != sizeCentralDir:[](#l2.56)
raise BadZipFile("Truncated central directory")[](#l2.57)
centdir = struct.unpack(structCentralDir, centdir)[](#l2.58)
if centdir[_CD_SIGNATURE] != stringCentralDir:[](#l2.59) raise BadZipFile("Bad magic number for central directory")[](#l2.60)
centdir = struct.unpack(structCentralDir, centdir)[](#l2.61) if self.debug > 2:[](#l2.62) print(centdir)[](#l2.63) filename = fp.read(centdir[_CD_FILENAME_LENGTH])[](#l2.64)
@@ -1123,10 +1134,12 @@ class ZipFile: # Skip the file header: fheader = zef_file.read(sizeFileHeader)
if fheader[0:4] != stringFileHeader:[](#l2.69)
if len(fheader) != sizeFileHeader:[](#l2.70)
raise BadZipFile("Truncated file header")[](#l2.71)
fheader = struct.unpack(structFileHeader, fheader)[](#l2.72)
if fheader[_FH_SIGNATURE] != stringFileHeader:[](#l2.73) raise BadZipFile("Bad magic number for file header")[](#l2.74)
fheader = struct.unpack(structFileHeader, fheader)[](#l2.76) fname = zef_file.read(fheader[_FH_FILENAME_LENGTH])[](#l2.77) if fheader[_FH_EXTRA_FIELD_LENGTH]:[](#l2.78) zef_file.read(fheader[_FH_EXTRA_FIELD_LENGTH])[](#l2.79)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -236,6 +236,10 @@ Core and Builtins Library ------- +- Issue #4844: ZipFile now raises BadZipFile when opens a ZIP file with an