]> granicus.if.org Git - python/commitdiff
bpo-32713: Fix tarfile.itn for large/negative float values. (GH-5434)
authorJoffrey F <f.joffrey@gmail.com>
Tue, 27 Feb 2018 00:02:21 +0000 (16:02 -0800)
committerSerhiy Storchaka <storchaka@gmail.com>
Tue, 27 Feb 2018 00:02:21 +0000 (02:02 +0200)
Lib/tarfile.py
Lib/test/test_tarfile.py
Misc/NEWS.d/next/Library/2018-02-26-13-16-36.bpo-32713.55yegW.rst [new file with mode: 0644]

index a24ee42abf82687836feb07ac026a88671516990..85119a48a48bfa220c8f249bf5d2499bb952e3df 100755 (executable)
@@ -200,8 +200,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])
index b868326d5c74a1f34fcc2086dc6a1fbf70fba66e..7d2eec8a7ccfaa7a0d475e3461e775c8b02ca6eb 100644 (file)
@@ -2149,6 +2149,14 @@ class MiscTest(unittest.TestCase):
         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 (file)
index 0000000..bb5d64a
--- /dev/null
@@ -0,0 +1 @@
+Fixed tarfile.itn handling of out-of-bounds float values. Patch by Joffrey Fuhrer.