Skip to content

Commit e6c5a9f

Browse files
committed
gh-84481: Make ZipFile._data_offset more robust
1 parent e247639 commit e6c5a9f

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

Lib/test/test_zipfile/test_core.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3348,6 +3348,12 @@ def test_data_offset_write_with_prefix(self):
33483348
with zipfile.ZipFile(fp, "w") as zipfp:
33493349
self.assertEqual(zipfp.data_offset, 16)
33503350

3351+
def test_data_offset_append_with_bad_zip(self):
3352+
with io.BytesIO() as fp:
3353+
fp.write(b"this is a prefix")
3354+
with zipfile.ZipFile(fp, "a") as zipfp:
3355+
self.assertEqual(zipfp.data_offset, 16)
3356+
33513357
def test_data_offset_write_no_tell(self):
33523358
# The initializer in ZipFile checks if tell raises AttributeError or
33533359
# OSError when creating a file in write mode when deducing the offset

Lib/zipfile/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,7 @@ def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=True,
14031403
self._lock = threading.RLock()
14041404
self._seekable = True
14051405
self._writing = False
1406+
self._data_offset = None
14061407

14071408
try:
14081409
if mode == 'r':
@@ -1418,7 +1419,6 @@ def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=True,
14181419
self.fp = _Tellable(self.fp)
14191420
self.start_dir = 0
14201421
self._seekable = False
1421-
self._data_offset = None
14221422
else:
14231423
# Some file-like objects can provide tell() but not seek()
14241424
try:
@@ -1439,6 +1439,7 @@ def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=True,
14391439
# even if no files are added to the archive
14401440
self._didModify = True
14411441
self.start_dir = self.fp.tell()
1442+
self._data_offset = self.start_dir
14421443
else:
14431444
raise ValueError("Mode must be 'r', 'w', 'x', or 'a'")
14441445
except:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy