[3.13] gh-107398: Fix tarfile stream mode exception when process the file with the gzip extra field (GH-126304) (GH-150201)

(cherry picked from commit 65f99329ed)

Co-authored-by: Nadeshiko Manju <me@manjusaka.me>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Miss Islington (bot) 2026-05-21 21:38:54 +02:00 committed by GitHub
parent 6f72734446
commit cfa3daa7fe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 33 additions and 3 deletions

View file

@ -489,7 +489,7 @@ def _init_read_gz(self):
if flag & 4:
xlen = ord(self.__read(1)) + 256 * ord(self.__read(1))
self.read(xlen)
self.__read(xlen)
if flag & 8:
while True:
s = self.__read(1)

View file

@ -884,10 +884,39 @@ def test_extractall_hardlink_on_symlink(self):
self._assert_on_file_content(hardlink_filepath, sha256_regtype)
class GzipReadTestBase:
def test_read_with_extra_field(self):
with open(self.tarname, 'rb') as f:
data = bytearray(f.read())
flags = data[3]
self.assertEqual(flags, 8)
data[3] = flags | 4
data[10:10] = b'\x05\x00extra'
with open(tmpname, 'wb') as f:
f.write(data)
print(self.mode)
with tarfile.open(tmpname, mode=self.mode):
pass
def test_read_with_file_comment(self):
with open(self.tarname, 'rb') as f:
data = bytearray(f.read())
flags = data[3]
self.assertEqual(flags, 8)
data[3] = flags | 16
i = data.index(0, 10) + 1
data[i:i] = b'comment\x00'
with open(tmpname, 'wb') as f:
f.write(data)
with tarfile.open(tmpname, mode=self.mode):
pass
class MiscReadTest(MiscReadTestBase, unittest.TestCase):
test_fail_comp = None
class GzipMiscReadTest(GzipTest, MiscReadTestBase, unittest.TestCase):
class GzipMiscReadTest(GzipTest, GzipReadTestBase, MiscReadTestBase, unittest.TestCase):
pass
class Bz2MiscReadTest(Bz2Test, MiscReadTestBase, unittest.TestCase):
@ -959,7 +988,7 @@ def test_compare_members(self):
finally:
tar1.close()
class GzipStreamReadTest(GzipTest, StreamReadTest):
class GzipStreamReadTest(GzipTest, GzipReadTestBase, StreamReadTest):
pass
class Bz2StreamReadTest(Bz2Test, StreamReadTest):

View file

@ -0,0 +1 @@
Fix :mod:`tarfile` stream mode exception when process the file with the gzip extra field.