]> granicus.if.org Git - python/commitdiff
Merged revisions 74245 via svnmerge from
authorR. David Murray <rdmurray@bitdance.com>
Wed, 6 Jan 2010 20:08:02 +0000 (20:08 +0000)
committerR. David Murray <rdmurray@bitdance.com>
Wed, 6 Jan 2010 20:08:02 +0000 (20:08 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r74245 | amaury.forgeotdarc | 2009-07-28 18:15:30 -0400 (Tue, 28 Jul 2009) | 3 lines

  #6511: ZipFile will now raise BadZipfile when opening an empty or tiny file,
  like it does for larger invalid files.
........

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

index 1fed79acae1c34fa5b24f23c4b8cce2cb48fb6a6..a2c3a2c3fe00abe74a7a01a968d306895386f132 100644 (file)
@@ -664,6 +664,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 testClosedZipRaisesRuntimeError(self):
         # Verify that testzip() doesn't swallow inappropriate exceptions.
         data = StringIO()
index e2ede459405d2f14c9449dbdad944592e70d68e8..34b3f1a7749896784720523664f2377d2ff9e457 100644 (file)
@@ -186,7 +186,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 b1a1af13aeb7dc815bdfe9add14416f1107a31e9..af2c9dd19f8e8a9bfcd365e06b35bfcb9398021c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -318,6 +318,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #6511: ZipFile now raises BadZipfile (instead of an IOError) when
+  opening an empty or very small file.
+
 - Issue #1488943: difflib.Differ() doesn't always add hints for tab characters
 
 - Issue #5037: Proxy the __unicode__ special method instead to __unicode__