Skip to content

Commit 5186da2

Browse files
committed
gh-141707: Fix tarfile type corruption with GNU long names
When processing GNU long name headers, the name field in the second header contains garbage data. The V7 directory detection logic in frombuf() would incorrectly mark regular files as directories if this garbage ended with '/'. Re-apply the detection after patching the actual long name to correct any type corruption.
1 parent 305aff0 commit 5186da2

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

Lib/tarfile.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,6 +1410,12 @@ def _proc_gnulong(self, tarfile):
14101410
next.offset = self.offset
14111411
if self.type == GNUTYPE_LONGNAME:
14121412
next.name = nts(buf, tarfile.encoding, tarfile.errors)
1413+
# V7 directory detection in frombuf() may have used garbage
1414+
# name data. Re-apply it with the actual long name.
1415+
if next.type == DIRTYPE and not next.name.endswith("/"):
1416+
next.type = AREGTYPE
1417+
elif next.type == AREGTYPE and next.name.endswith("/"):
1418+
next.type = DIRTYPE
14131419
elif self.type == GNUTYPE_LONGLINK:
14141420
next.linkname = nts(buf, tarfile.encoding, tarfile.errors)
14151421

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :mod:`tarfile` type corruption when extracting files with GNU long names. Regular files were incorrectly identified as directories if the header's name field ended with '/'.

0 commit comments

Comments
 (0)