Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -490,25 +490,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("<BB6x", 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the only relevant change is in this line.

# 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: 6 additions & 0 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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 @@ -876,6 +877,11 @@ def test_read_through(self):
if not buf:
break

@unittest.skipIf(zlib is None, "requires zlib")
def test_read_with_extra_header(self):
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 :mod:`tarfile` stream mode exception when process the file with extra header.
data.
Loading