]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-107396: tarfiles: set self.exception before _init_read_gz() (GH-107485...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 21 Aug 2023 15:16:59 +0000 (08:16 -0700)
committerGitHub <noreply@github.com>
Mon, 21 Aug 2023 15:16:59 +0000 (17:16 +0200)
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 37135d25e269ede92bc7da363bebfa574782e59a)

Co-authored-by: balmeida-nokia <83089745+balmeida-nokia@users.noreply.github.com>
Lib/tarfile.py
Lib/test/test_tarfile.py
Misc/NEWS.d/next/Library/2023-07-31-07-36-24.gh-issue-107396.3_Kh6D.rst [new file with mode: 0644]

index 130b5e0f45dcd5531f5f4b32b67a7371b278de05..b7adff6e1723bc83eb42be0017efa34f76b12fbf 100755 (executable)
@@ -372,8 +372,8 @@ class _Stream:
                 self.zlib = zlib
                 self.crc = zlib.crc32(b"")
                 if mode == "r":
-                    self._init_read_gz()
                     self.exception = zlib.error
+                    self._init_read_gz()
                 else:
                     self._init_write_gz()
 
index cdea033ec1244add810b28704bd641be1ca5e53f..dc7ff852363cf596539fe1ab3c93551710b65deb 100644 (file)
@@ -908,6 +908,23 @@ class LzmaDetectReadTest(LzmaTest, DetectReadTest):
     pass
 
 
+class GzipBrokenHeaderCorrectException(GzipTest, unittest.TestCase):
+    """
+    See: https://github.com/python/cpython/issues/107396
+    """
+    def runTest(self):
+        f = io.BytesIO(
+            b'\x1f\x8b'  # header
+            b'\x08'  # compression method
+            b'\x04'  # flags
+            b'\0\0\0\0\0\0'  # timestamp, compression data, OS ID
+            b'\0\x01'  # size
+            b'\0\0\0\0\0'  # corrupt data (zeros)
+        )
+        with self.assertRaises(tarfile.ReadError):
+            tarfile.open(fileobj=f, mode='r|gz')
+
+
 class MemberReadTest(ReadTest, unittest.TestCase):
 
     def _test_member(self, tarinfo, chksum=None, **kwargs):
diff --git a/Misc/NEWS.d/next/Library/2023-07-31-07-36-24.gh-issue-107396.3_Kh6D.rst b/Misc/NEWS.d/next/Library/2023-07-31-07-36-24.gh-issue-107396.3_Kh6D.rst
new file mode 100644 (file)
index 0000000..73bff4b
--- /dev/null
@@ -0,0 +1 @@
+tarfiles; Fixed use before assignment of self.exception for gzip decompression