]> granicus.if.org Git - python/commitdiff
#6511: ZipFile will now raise BadZipfile when opening an empty or tiny file,
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Tue, 28 Jul 2009 22:15:30 +0000 (22:15 +0000)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Tue, 28 Jul 2009 22:15:30 +0000 (22:15 +0000)
like it does for larger invalid files.

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

index ee1eecb983d0069f699aafa51bd55bff2e3849f3..652274f8fe37a92867568b3908383b9ee266b628 100644 (file)
@@ -703,6 +703,16 @@ class OtherTests(unittest.TestCase):
         # quickly.
         self.assertRaises(IOError, zipfile.ZipFile, TESTFN)
 
+    def test_empty_file_raises_BadZipFile(self):
+        f = open(TESTFN, 'w')
+        f.close()
+        self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
+
+        f = open(TESTFN, 'w')
+        f.write("short file")
+        f.close()
+        self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
+
     def test_closed_zip_raises_RuntimeError(self):
         # Verify that testzip() doesn't swallow inappropriate exceptions.
         data = StringIO()
index 6ee9923e7963ea939c4052f84679ee964f1619bb..2aa4bffc4281c6f305d581e82ed6bd9a2428e2b9 100644 (file)
@@ -198,7 +198,10 @@ def _EndRecData(fpin):
     # Check to see if this is ZIP file with no archive comment (the
     # "end of central directory" structure should be the last item in the
     # file if this is the case).
-    fpin.seek(-sizeEndCentDir, 2)
+    try:
+        fpin.seek(-sizeEndCentDir, 2)
+    except IOError:
+        return None
     data = fpin.read()
     if data[0:4] == stringEndArchive and data[-2:] == "\000\000":
         # the signature is correct and there's no comment, unpack structure
index 6731fb93e16e00669c3ef109daed8cee5e36e4bc..37ccbeb40608d45b5242d43047325d7431fa9954 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -354,6 +354,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #6511: ZipFile now raises BadZipfile (instead of an IOError) when
+  opening an empty or very small file.
+
 - Issue #6553: Fixed a crash in cPickle.load(), when given a file-like object
   containing incomplete data.