]> granicus.if.org Git - python/commitdiff
#10694: zipfile now ignores garbage at the end of a zipfile.
authorR David Murray <rdmurray@bitdance.com>
Thu, 9 Jun 2011 19:50:51 +0000 (15:50 -0400)
committerR David Murray <rdmurray@bitdance.com>
Thu, 9 Jun 2011 19:50:51 +0000 (15:50 -0400)
Original fix by 'rep', final patch (with tests) by Xuanji Li.

Lib/test/test_zipfile.py
Lib/zipfile.py
Misc/NEWS

index 90aab8689cde7933028265679b60f41dc9d20566..782020c6c84c1f000f30556b19c094cdf1087e3e 100644 (file)
@@ -351,6 +351,24 @@ class TestsWithSourceFile(unittest.TestCase):
             with zipfile.ZipFile(f, "r") as zipfp:
                 self.assertEqual(zipfp.namelist(), [TESTFN])
 
+    def test_ignores_newline_at_end(self):
+        with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
+            zipfp.write(TESTFN, TESTFN)
+        with open(TESTFN2, 'a') as f:
+            f.write("\r\n\00\00\00")
+        with zipfile.ZipFile(TESTFN2, "r") as zipfp:
+            self.assertIsInstance(zipfp, zipfile.ZipFile)
+
+    def test_ignores_stuff_appended_past_comments(self):
+        with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
+            zipfp.comment = b"this is a comment"
+            zipfp.write(TESTFN, TESTFN)
+        with open(TESTFN2, 'a') as f:
+            f.write("abcdef\r\n")
+        with zipfile.ZipFile(TESTFN2, "r") as zipfp:
+            self.assertIsInstance(zipfp, zipfile.ZipFile)
+            self.assertEqual(zipfp.comment, b"this is a comment")
+
     def test_write_default_name(self):
         """Check that calling ZipFile.write without arcname specified
         produces the expected result."""
index 50f484873ad85990fe76a624fded11a269e99392..5cc7816e0c383c53f1c6bf00ede26cb4da6e0124 100644 (file)
@@ -246,16 +246,14 @@ def _EndRecData(fpin):
         # found the magic number; attempt to unpack and interpret
         recData = data[start:start+sizeEndCentDir]
         endrec = list(struct.unpack(structEndArchive, recData))
-        comment = data[start+sizeEndCentDir:]
-        # check that comment length is correct
-        if endrec[_ECD_COMMENT_SIZE] == len(comment):
-            # Append the archive comment and start offset
-            endrec.append(comment)
-            endrec.append(maxCommentStart + start)
-
-            # Try to read the "Zip64 end of central directory" structure
-            return _EndRecData64(fpin, maxCommentStart + start - filesize,
-                                 endrec)
+        commentSize = endrec[_ECD_COMMENT_SIZE] #as claimed by the zip file
+        comment = data[start+sizeEndCentDir:start+sizeEndCentDir+commentSize]
+        endrec.append(comment)
+        endrec.append(maxCommentStart + start)
+
+        # Try to read the "Zip64 end of central directory" structure
+        return _EndRecData64(fpin, maxCommentStart + start - filesize,
+                             endrec)
 
     # Unable to find a valid end of central directory structure
     return
index dab5355eddb7c936dcfba4630143f223a41c82cd..2d00324e2ea68e185e0af8e01c7bee19b2f5083d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -22,6 +22,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #10694: zipfile now ignores garbage at the end of a zipfile.
+
 - Issue #12283: Fixed regression in smtplib quoting of leading dots in DATA.
 
 - Issue #12168: SysLogHandler now allows NUL termination to be controlled using