Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-107398: Fix tarfile stream mode exception when process the file with extra header data #126304

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,25 +480,27 @@ def _init_read_gz(self):
# taken from gzip.GzipFile with some alterations
if self.__read(2) != b"\037\213":
raise ReadError("not a gzip file")
if self.__read(1) != b"\010":
(method, flag, _) = struct.unpack("<BBIxx", self.__read(8))
if method != 8:
raise CompressionError("unsupported compression method")

flag = ord(self.__read(1))
self.__read(6)

# process FEXTRA
if flag & 4:
xlen = ord(self.__read(1)) + 256 * ord(self.__read(1))
self.read(xlen)
extra_len, = struct.unpack("<H", self.__read(2))
self.__read(extra_len)
# process FNAME
if flag & 8:
while True:
s = self.__read(1)
if not s or s == NUL:
if not s or s == b'\000':
break
# process FCOMMENT
if flag & 16:
while True:
s = self.__read(1)
if not s or s == NUL:
if not s or s == b'\000':
break
# process FHCRC
if flag & 2:
self.__read(2)

Expand Down
Binary file not shown.
6 changes: 5 additions & 1 deletion Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def sha256sum(data):
TEMPDIR = os.path.abspath(os_helper.TESTFN) + "-tardir"
tarextdir = TEMPDIR + '-extract-test'
tarname = support.findfile("testtar.tar", subdir="archivetestdata")
tgzname_with_comment_extra_data_in_header = support.findfile("tgz_with_comment_extra_data_in_header.tgz", subdir="archivetestdata")
gzipname = os.path.join(TEMPDIR, "testtar.tar.gz")
bz2name = os.path.join(TEMPDIR, "testtar.tar.bz2")
xzname = os.path.join(TEMPDIR, "testtar.tar.xz")
Expand Down Expand Up @@ -858,7 +859,10 @@ def test_read_through(self):
"TarFile.extractfile() failed")
if not buf:
break

def test_read_with_extra_header(self):
Zheaoli marked this conversation as resolved.
Show resolved Hide resolved
with tarfile.open(tgzname_with_comment_extra_data_in_header,
mode="r|*") as _:
pass
def test_fileobj_regular_file(self):
tarinfo = self.tar.next() # get "regtype" (can't use getmember)
with self.tar.extractfile(tarinfo) as fobj:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix tarfile stream mode exception when process the file with extra header
Zheaoli marked this conversation as resolved.
Show resolved Hide resolved
data.
Loading