mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
[3.10] gh-83245: Raise BadZipFile instead of ValueError when reading a corrupt ZIP file (GH-32291) (GH-93140)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
(cherry picked from commit 202ed2506c
)
Co-authored-by: Sam Ezeh <sam.z.ezeh@gmail.com>
Automerge-Triggered-By: GH:serhiy-storchaka
This commit is contained in:
parent
2d5d01f26b
commit
132ea29936
3 changed files with 15 additions and 0 deletions
|
@ -1736,6 +1736,17 @@ class OtherTests(unittest.TestCase):
|
|||
fp.write("short file")
|
||||
self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN)
|
||||
|
||||
def test_negative_central_directory_offset_raises_BadZipFile(self):
|
||||
# Zip file containing an empty EOCD record
|
||||
buffer = bytearray(b'PK\x05\x06' + b'\0'*18)
|
||||
|
||||
# Set the size of the central directory bytes to become 1,
|
||||
# causing the central directory offset to become negative
|
||||
for dirsize in 1, 2**32-1:
|
||||
buffer[12:16] = struct.pack('<L', dirsize)
|
||||
f = io.BytesIO(buffer)
|
||||
self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, f)
|
||||
|
||||
def test_closed_zip_raises_ValueError(self):
|
||||
"""Verify that testzip() doesn't swallow inappropriate exceptions."""
|
||||
data = io.BytesIO()
|
||||
|
|
|
@ -1349,6 +1349,8 @@ class ZipFile:
|
|||
print("given, inferred, offset", offset_cd, inferred, concat)
|
||||
# self.start_dir: Position of start of central directory
|
||||
self.start_dir = offset_cd + concat
|
||||
if self.start_dir < 0:
|
||||
raise BadZipFile("Bad offset for central directory")
|
||||
fp.seek(self.start_dir, 0)
|
||||
data = fp.read(size_cd)
|
||||
fp = io.BytesIO(data)
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
:class:`zipfile.ZipFile` now raises :exc:`zipfile.BadZipFile` instead of ``ValueError`` when reading a
|
||||
corrupt zip file in which the central directory offset is negative.
|
Loading…
Add table
Add a link
Reference in a new issue