Skip to content

Commit 7689952

Browse files
miss-islingtonZheaoliserhiy-storchaka
authored
[3.14] gh-107398: Fix tarfile stream mode exception when process the file with the gzip extra field (GH-126304) (GH-150200)
(cherry picked from commit 65f9932) Co-authored-by: Nadeshiko Manju <me@manjusaka.me> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent c863e96 commit 7689952

3 files changed

Lines changed: 33 additions & 3 deletions

File tree

Lib/tarfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ def _init_read_gz(self):
498498

499499
if flag & 4:
500500
xlen = ord(self.__read(1)) + 256 * ord(self.__read(1))
501-
self.read(xlen)
501+
self.__read(xlen)
502502
if flag & 8:
503503
while True:
504504
s = self.__read(1)

Lib/test/test_tarfile.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -892,10 +892,39 @@ def test_extractall_hardlink_on_symlink(self):
892892
self._assert_on_file_content(hardlink_filepath, sha256_regtype)
893893

894894

895+
class GzipReadTestBase:
896+
897+
def test_read_with_extra_field(self):
898+
with open(self.tarname, 'rb') as f:
899+
data = bytearray(f.read())
900+
flags = data[3]
901+
self.assertEqual(flags, 8)
902+
data[3] = flags | 4
903+
data[10:10] = b'\x05\x00extra'
904+
with open(tmpname, 'wb') as f:
905+
f.write(data)
906+
print(self.mode)
907+
with tarfile.open(tmpname, mode=self.mode):
908+
pass
909+
910+
def test_read_with_file_comment(self):
911+
with open(self.tarname, 'rb') as f:
912+
data = bytearray(f.read())
913+
flags = data[3]
914+
self.assertEqual(flags, 8)
915+
data[3] = flags | 16
916+
i = data.index(0, 10) + 1
917+
data[i:i] = b'comment\x00'
918+
with open(tmpname, 'wb') as f:
919+
f.write(data)
920+
with tarfile.open(tmpname, mode=self.mode):
921+
pass
922+
923+
895924
class MiscReadTest(MiscReadTestBase, unittest.TestCase):
896925
test_fail_comp = None
897926

898-
class GzipMiscReadTest(GzipTest, MiscReadTestBase, unittest.TestCase):
927+
class GzipMiscReadTest(GzipTest, GzipReadTestBase, MiscReadTestBase, unittest.TestCase):
899928
pass
900929

901930
class Bz2MiscReadTest(Bz2Test, MiscReadTestBase, unittest.TestCase):
@@ -969,7 +998,7 @@ def test_compare_members(self):
969998
finally:
970999
tar1.close()
9711000

972-
class GzipStreamReadTest(GzipTest, StreamReadTest):
1001+
class GzipStreamReadTest(GzipTest, GzipReadTestBase, StreamReadTest):
9731002
pass
9741003

9751004
class Bz2StreamReadTest(Bz2Test, StreamReadTest):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :mod:`tarfile` stream mode exception when process the file with the gzip extra field.

0 commit comments

Comments
 (0)