Skip to content

Commit ac3d137

Browse files
committed
Issue #13158: Fix decoding and encoding of base-256 number fields in tarfile.
The nti() function that converts a number field from a tar header to a number failed to decode GNU tar specific base-256 fields. I also added support for decoding and encoding negative base-256 number fields.
1 parent 82ac9bc commit ac3d137

3 files changed

Lines changed: 49 additions & 21 deletions

File tree

‎Lib/tarfile.py‎

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,18 @@ def nti(s):
196196
"""
197197
# There are two possible encodings for a number field, see
198198
# itn() below.
199-
if s[0] != chr(0o200):
199+
if s[0] in (0o200, 0o377):
200+
n = 0
201+
for i in range(len(s) - 1):
202+
n <<= 8
203+
n += s[i + 1]
204+
if s[0] == 0o377:
205+
n = -(256 ** (len(s) - 1) - n)
206+
else:
200207
try:
201208
n = int(nts(s, "ascii", "strict") or "0", 8)
202209
except ValueError:
203210
raise InvalidHeaderError("invalid header")
204-
else:
205-
n = 0
206-
for i in range(len(s) - 1):
207-
n <<= 8
208-
n += ord(s[i + 1])
209211
return n
210212

211213
def itn(n, digits=8, format=DEFAULT_FORMAT):
@@ -214,25 +216,26 @@ def itn(n, digits=8, format=DEFAULT_FORMAT):
214216
# POSIX 1003.1-1988 requires numbers to be encoded as a string of
215217
# octal digits followed by a null-byte, this allows values up to
216218
# (8**(digits-1))-1. GNU tar allows storing numbers greater than
217-
# that if necessary. A leading 0o200 byte indicates this particular
218-
# encoding, the following digits-1 bytes are a big-endian
219-
# representation. This allows values up to (256**(digits-1))-1.
219+
# that if necessary. A leading 0o200 or 0o377 byte indicate this
220+
# particular encoding, the following digits-1 bytes are a big-endian
221+
# base-256 representation. This allows values up to (256**(digits-1))-1.
222+
# A 0o200 byte indicates a positive number, a 0o377 byte a negative
223+
# number.
220224
if 0 <= n < 8 ** (digits - 1):
221225
s = bytes("%0*o" % (digits - 1, n), "ascii") + NUL
222-
else:
223-
if format != GNU_FORMAT or n >= 256 ** (digits - 1):
224-
raise ValueError("overflow in number field")
225-
226-
if n < 0:
227-
# XXX We mimic GNU tar's behaviour with negative numbers,
228-
# this could raise OverflowError.
229-
n = struct.unpack("L", struct.pack("l", n))[0]
226+
elif format == GNU_FORMAT and -256 ** (digits - 1) <= n < 256 ** (digits - 1):
227+
if n >= 0:
228+
s = bytearray([0o200])
229+
else:
230+
s = bytearray([0o377])
231+
n = 256 ** digits + n
230232

231-
s = bytearray()
232233
for i in range(digits - 1):
233-
s.insert(0, n & 0o377)
234+
s.insert(1, n & 0o377)
234235
n >>= 8
235-
s.insert(0, 0o200)
236+
else:
237+
raise ValueError("overflow in number field")
238+
236239
return s
237240

238241
def calc_chksums(buf):

‎Lib/test/test_tarfile.py‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1582,9 +1582,31 @@ def test_char_fields(self):
15821582
self.assertEqual(tarfile.nts(b"foo\0\0\0\0\0", "ascii", "strict"), "foo")
15831583
self.assertEqual(tarfile.nts(b"foo\0bar\0", "ascii", "strict"), "foo")
15841584

1585-
def test_number_fields(self):
1585+
def test_read_number_fields(self):
1586+
# Issue 13158: Test if GNU tar specific base-256 number fields
1587+
# are decoded correctly.
1588+
self.assertEqual(tarfile.nti(b"0000001\x00"), 1)
1589+
self.assertEqual(tarfile.nti(b"7777777\x00"), 0o7777777)
1590+
self.assertEqual(tarfile.nti(b"\x80\x00\x00\x00\x00\x20\x00\x00"), 0o10000000)
1591+
self.assertEqual(tarfile.nti(b"\x80\x00\x00\x00\xff\xff\xff\xff"), 0xffffffff)
1592+
self.assertEqual(tarfile.nti(b"\xff\xff\xff\xff\xff\xff\xff\xff"), -1)
1593+
self.assertEqual(tarfile.nti(b"\xff\xff\xff\xff\xff\xff\xff\x9c"), -100)
1594+
self.assertEqual(tarfile.nti(b"\xff\x00\x00\x00\x00\x00\x00\x00"), -0x100000000000000)
1595+
1596+
def test_write_number_fields(self):
15861597
self.assertEqual(tarfile.itn(1), b"0000001\x00")
1598+
self.assertEqual(tarfile.itn(0o7777777), b"7777777\x00")
1599+
self.assertEqual(tarfile.itn(0o10000000), b"\x80\x00\x00\x00\x00\x20\x00\x00")
15871600
self.assertEqual(tarfile.itn(0xffffffff), b"\x80\x00\x00\x00\xff\xff\xff\xff")
1601+
self.assertEqual(tarfile.itn(-1), b"\xff\xff\xff\xff\xff\xff\xff\xff")
1602+
self.assertEqual(tarfile.itn(-100), b"\xff\xff\xff\xff\xff\xff\xff\x9c")
1603+
self.assertEqual(tarfile.itn(-0x100000000000000), b"\xff\x00\x00\x00\x00\x00\x00\x00")
1604+
1605+
def test_number_field_limits(self):
1606+
self.assertRaises(ValueError, tarfile.itn, -1, 8, tarfile.USTAR_FORMAT)
1607+
self.assertRaises(ValueError, tarfile.itn, 0o10000000, 8, tarfile.USTAR_FORMAT)
1608+
self.assertRaises(ValueError, tarfile.itn, -0x10000000001, 6, tarfile.GNU_FORMAT)
1609+
self.assertRaises(ValueError, tarfile.itn, 0x10000000000, 6, tarfile.GNU_FORMAT)
15881610

15891611

15901612
class ContextManagerTest(unittest.TestCase):

‎Misc/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Core and Builtins
4343
Library
4444
-------
4545

46+
- Issue #13158: Fix decoding and encoding of GNU tar specific base-256 number
47+
fields in tarfile.
48+
4649
- Issue #13025: mimetypes is now reading MIME types using the UTF-8 encoding,
4750
instead of the locale encoding.
4851

0 commit comments

Comments
 (0)