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

Skip to content

Commit b7a688b

Browse files
committed
Issue #24514: tarfile now tolerates number fields consisting of only whitespace.
1 parent 3ef8058 commit b7a688b

3 files changed

Lines changed: 9 additions & 1 deletion

File tree

Lib/tarfile.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ def nti(s):
178178
n = -(256 ** (len(s) - 1) - n)
179179
else:
180180
try:
181-
n = int(nts(s, "ascii", "strict") or "0", 8)
181+
s = nts(s, "ascii", "strict")
182+
n = int(s.strip() or "0", 8)
182183
except ValueError:
183184
raise InvalidHeaderError("invalid header")
184185
return n

Lib/test/test_tarfile.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,6 +1842,10 @@ def test_read_number_fields(self):
18421842
self.assertEqual(tarfile.nti(b"\xff\x00\x00\x00\x00\x00\x00\x00"),
18431843
-0x100000000000000)
18441844

1845+
# Issue 24514: Test if empty number fields are converted to zero.
1846+
self.assertEqual(tarfile.nti(b"\0"), 0)
1847+
self.assertEqual(tarfile.nti(b" \0"), 0)
1848+
18451849
def test_write_number_fields(self):
18461850
self.assertEqual(tarfile.itn(1), b"0000001\x00")
18471851
self.assertEqual(tarfile.itn(0o7777777), b"7777777\x00")

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ Core and Builtins
6464
Library
6565
-------
6666

67+
- Issue #24514: tarfile now tolerates number fields consisting of only
68+
whitespace.
69+
6770
- Issue #19176: Fixed doctype() related bugs in C implementation of ElementTree.
6871
A deprecation warning no longer issued by XMLParser subclass with default
6972
doctype() method. Direct call of doctype() now issues a warning. Parser's

0 commit comments

Comments
 (0)