Skip to content

Commit 72d9b2b

Browse files
shin-serhiy-storchaka
authored andcommitted
bpo-32713: Fix tarfile.itn for large/negative float values. (GH-5434)
1 parent eee72d4 commit 72d9b2b

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

Lib/tarfile.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,9 @@ def itn(n, digits=8, format=DEFAULT_FORMAT):
200200
# base-256 representation. This allows values up to (256**(digits-1))-1.
201201
# A 0o200 byte indicates a positive number, a 0o377 byte a negative
202202
# number.
203+
n = int(n)
203204
if 0 <= n < 8 ** (digits - 1):
204-
s = bytes("%0*o" % (digits - 1, int(n)), "ascii") + NUL
205+
s = bytes("%0*o" % (digits - 1, n), "ascii") + NUL
205206
elif format == GNU_FORMAT and -256 ** (digits - 1) <= n < 256 ** (digits - 1):
206207
if n >= 0:
207208
s = bytearray([0o200])

Lib/test/test_tarfile.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,6 +2149,14 @@ def test_write_number_fields(self):
21492149
self.assertEqual(tarfile.itn(-0x100000000000000),
21502150
b"\xff\x00\x00\x00\x00\x00\x00\x00")
21512151

2152+
# Issue 32713: Test if itn() supports float values outside the
2153+
# non-GNU format range
2154+
self.assertEqual(tarfile.itn(-100.0, format=tarfile.GNU_FORMAT),
2155+
b"\xff\xff\xff\xff\xff\xff\xff\x9c")
2156+
self.assertEqual(tarfile.itn(8 ** 12 + 0.0, format=tarfile.GNU_FORMAT),
2157+
b"\x80\x00\x00\x10\x00\x00\x00\x00")
2158+
self.assertEqual(tarfile.nti(tarfile.itn(-0.1, format=tarfile.GNU_FORMAT)), 0)
2159+
21522160
def test_number_field_limits(self):
21532161
with self.assertRaises(ValueError):
21542162
tarfile.itn(-1, 8, tarfile.USTAR_FORMAT)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed tarfile.itn handling of out-of-bounds float values. Patch by Joffrey Fuhrer.

0 commit comments

Comments
 (0)