Skip to content

Commit f0ede88

Browse files
Merge branch 'main' into fix-150075-tar-addfile-no-offsets
2 parents c29360b + 65f9932 commit f0ede88

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
@@ -893,10 +893,39 @@ def test_extractall_hardlink_on_symlink(self):
893893
self._assert_on_file_content(hardlink_filepath, sha256_regtype)
894894

895895

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

899-
class GzipMiscReadTest(GzipTest, MiscReadTestBase, unittest.TestCase):
928+
class GzipMiscReadTest(GzipTest, GzipReadTestBase, MiscReadTestBase, unittest.TestCase):
900929
pass
901930

902931
class Bz2MiscReadTest(Bz2Test, MiscReadTestBase, unittest.TestCase):
@@ -970,7 +999,7 @@ def test_compare_members(self):
970999
finally:
9711000
tar1.close()
9721001

973-
class GzipStreamReadTest(GzipTest, StreamReadTest):
1002+
class GzipStreamReadTest(GzipTest, GzipReadTestBase, StreamReadTest):
9741003
pass
9751004

9761005
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)