Skip to content

Commit e1b069f

Browse files
[3.12] gh-107396: tarfiles: set self.exception before _init_read_gz() (GH-107485) (#108207)
gh-107396: tarfiles: set self.exception before _init_read_gz() (GH-107485) In the stack call of: _init_read_gz() ``` _read, tarfile.py:548 read, tarfile.py:526 _init_read_gz, tarfile.py:491 ``` a try;except exists that uses `self.exception`, so it needs to be set before calling _init_read_gz(). (cherry picked from commit 37135d2) Co-authored-by: balmeida-nokia <[email protected]>
1 parent 238c8d2 commit e1b069f

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

Lib/tarfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,8 @@ def __init__(self, name, mode, comptype, fileobj, bufsize,
372372
self.zlib = zlib
373373
self.crc = zlib.crc32(b"")
374374
if mode == "r":
375-
self._init_read_gz()
376375
self.exception = zlib.error
376+
self._init_read_gz()
377377
else:
378378
self._init_write_gz(compresslevel)
379379

Lib/test/test_tarfile.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,23 @@ class LzmaDetectReadTest(LzmaTest, DetectReadTest):
915915
pass
916916

917917

918+
class GzipBrokenHeaderCorrectException(GzipTest, unittest.TestCase):
919+
"""
920+
See: https://github.com/python/cpython/issues/107396
921+
"""
922+
def runTest(self):
923+
f = io.BytesIO(
924+
b'\x1f\x8b' # header
925+
b'\x08' # compression method
926+
b'\x04' # flags
927+
b'\0\0\0\0\0\0' # timestamp, compression data, OS ID
928+
b'\0\x01' # size
929+
b'\0\0\0\0\0' # corrupt data (zeros)
930+
)
931+
with self.assertRaises(tarfile.ReadError):
932+
tarfile.open(fileobj=f, mode='r|gz')
933+
934+
918935
class MemberReadTest(ReadTest, unittest.TestCase):
919936

920937
def _test_member(self, tarinfo, chksum=None, **kwargs):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tarfiles; Fixed use before assignment of self.exception for gzip decompression

0 commit comments

Comments
 (0)