[Python-checkins] cpython (3.2): #10694: zipfile now ignores garbage at the end of a zipfile.

r.david.murray python-checkins at python.org
Thu Jun 9 22:02:06 CEST 2011


http://hg.python.org/cpython/rev/7530b141c20f
changeset: 70746:7530b141c20f
branch: 3.2
parent: 70744:077b016e4a6d
user: R David Murray <rdmurray at bitdance.com>
date: Thu Jun 09 15:50:51 2011 -0400
summary:
 #10694: zipfile now ignores garbage at the end of a zipfile.
Original fix by 'rep', final patch (with tests) by Xuanji Li.
files:
 Lib/test/test_zipfile.py | 18 ++++++++++++++++++
 Lib/zipfile.py | 16 +++++++---------
 Misc/NEWS | 2 ++
 3 files changed, 27 insertions(+), 9 deletions(-)
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -351,6 +351,24 @@
 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\n00円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."""
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -246,16 +246,14 @@
 # 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)
+ 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)
+ # 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
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -22,6 +22,8 @@
 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
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /