Skip to content
Merged
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
3 changes: 2 additions & 1 deletion Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,9 @@ def itn(n, digits=8, format=DEFAULT_FORMAT):
# base-256 representation. This allows values up to (256**(digits-1))-1.
# A 0o200 byte indicates a positive number, a 0o377 byte a negative
# number.
n = int(n)
if 0 <= n < 8 ** (digits - 1):
s = bytes("%0*o" % (digits - 1, int(n)), "ascii") + NUL
s = bytes("%0*o" % (digits - 1, n), "ascii") + NUL
elif format == GNU_FORMAT and -256 ** (digits - 1) <= n < 256 ** (digits - 1):
if n >= 0:
s = bytearray([0o200])
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2146,6 +2146,14 @@ def test_write_number_fields(self):
self.assertEqual(tarfile.itn(-0x100000000000000),
b"\xff\x00\x00\x00\x00\x00\x00\x00")

# Issue 32713: Test if itn() supports float values outside the
# non-GNU format range
self.assertEqual(tarfile.itn(-100.0, format=tarfile.GNU_FORMAT),
b"\xff\xff\xff\xff\xff\xff\xff\x9c")
self.assertEqual(tarfile.itn(8 ** 12 + 0.0, format=tarfile.GNU_FORMAT),
b"\x80\x00\x00\x10\x00\x00\x00\x00")
self.assertEqual(tarfile.nti(tarfile.itn(-0.1, format=tarfile.GNU_FORMAT)), 0)

def test_number_field_limits(self):
with self.assertRaises(ValueError):
tarfile.itn(-1, 8, tarfile.USTAR_FORMAT)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed tarfile.itn handling of out-of-bounds float values. Patch by Joffrey Fuhrer.