Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e730ae7

Browse files
authored
bpo-42369: Fix thread safety of zipfile._SharedFile.tell (GH-26974)
The `_SharedFile` tracks its own virtual position into the file as `self._pos` and updates it after reading or seeking. `tell()` should return this position instead of calling into the underlying file object, since if multiple `_SharedFile` instances are being used concurrently on the same file, another one may have moved the real file position. Additionally, calling into the underlying `tell` may expose thread safety issues in the underlying file object because it was called without taking the lock.
1 parent 3af68fc commit e730ae7

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

Lib/zipfile.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,9 @@ def __init__(self, file, pos, close, lock, writing):
747747
self._lock = lock
748748
self._writing = writing
749749
self.seekable = file.seekable
750-
self.tell = file.tell
750+
751+
def tell(self):
752+
return self._pos
751753

752754
def seek(self, offset, whence=0):
753755
with self._lock:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix thread safety of :meth:`zipfile._SharedFile.tell` to avoid a "zipfile.BadZipFile: Bad CRC-32 for file" exception when reading a :class:`ZipFile` from multiple threads.

0 commit comments

Comments
 (0)