Skip to content

Commit 1fa6ef2

Browse files
committed
bpo-39017 Fix infinite loop in the tarfile module
Add a check for length = 0 in the _proc_pax function to avoid running into an infinite loop
1 parent 344dce3 commit 1fa6ef2

3 files changed

Lines changed: 7 additions & 0 deletions

File tree

Lib/tarfile.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,8 @@ def _proc_pax(self, tarfile):
12491249

12501250
length, keyword = match.groups()
12511251
length = int(length)
1252+
if length == 0:
1253+
raise InvalidHeaderError("invalid header")
12521254
value = buf[match.end(2) + 1:match.start(1) + length - 1]
12531255

12541256
# Normally, we could just use "utf-8" as the encoding and "strict"

Lib/test/recursion.tar

516 Bytes
Binary file not shown.

Lib/test/test_tarfile.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,11 @@ def test_premature_end_of_archive(self):
429429
with self.assertRaisesRegex(tarfile.ReadError, "unexpected end of data"):
430430
tar.extractfile(t).read()
431431

432+
def test_length_zero_header(self):
433+
with self.assertRaisesRegex(tarfile.ReadError, "file could not be opened successfully"):
434+
with tarfile.open(support.findfile('recursion.tar')) as tar:
435+
tar.getmembers()
436+
432437
class MiscReadTestBase(CommonReadTest):
433438
def requires_name_attribute(self):
434439
pass

0 commit comments

Comments
 (0)