From 202ed2506c84cd98e9e35621b5b2929ceb717864 Mon Sep 17 00:00:00 2001 From: Sam Ezeh Date: Mon, 23 May 2022 18:59:21 +0100 Subject: gh-83245: Raise BadZipFile instead of ValueError when reading a corrupt ZIP file (GH-32291) Co-authored-by: Serhiy Storchaka --- Lib/test/test_zipfile.py | 11 +++++++++++ Lib/zipfile.py | 2 ++ .../next/Library/2022-04-03-19-40-09.bpo-39064.76PbIz.rst | 2 ++ 3 files changed, 15 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2022-04-03-19-40-09.bpo-39064.76PbIz.rst diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index 848bf4f..f4c11d8 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -1740,6 +1740,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('