[Python-checkins] bpo-32713: Fix tarfile.itn for large/negative float values. (GH-5434)

Miss Islington (bot) webhook-mailer at python.org
Mon Feb 26 20:11:11 EST 2018


https://github.com/python/cpython/commit/3f6c172b702dccc90d16db8faa14b4fabda63ff6
commit: 3f6c172b702dccc90d16db8faa14b4fabda63ff6
branch: 3.6
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-02-26T17:11:01-08:00
summary:

bpo-32713: Fix tarfile.itn for large/negative float values. (GH-5434)

(cherry picked from commit 72d9b2be36f091793ae7ffc5ad751f040c6e6ad3)

Co-authored-by: Joffrey F <f.joffrey at gmail.com>

files:
A Misc/NEWS.d/next/Library/2018-02-26-13-16-36.bpo-32713.55yegW.rst
M Lib/tarfile.py
M Lib/test/test_tarfile.py

diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 631b69dcba4f..395b846ee2c4 100755
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -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])
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index fc7905542116..4cd7d5370f58 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -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)
diff --git a/Misc/NEWS.d/next/Library/2018-02-26-13-16-36.bpo-32713.55yegW.rst b/Misc/NEWS.d/next/Library/2018-02-26-13-16-36.bpo-32713.55yegW.rst
new file mode 100644
index 000000000000..bb5d64a351cb
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-02-26-13-16-36.bpo-32713.55yegW.rst
@@ -0,0 +1 @@
+Fixed tarfile.itn handling of out-of-bounds float values. Patch by Joffrey Fuhrer.



More information about the Python-checkins mailing list