]> granicus.if.org Git - python/commitdiff
Issue #18137: Detect integer overflow on precision in float.__format__() and
authorVictor Stinner <victor.stinner@gmail.com>
Sun, 23 Jun 2013 12:54:30 +0000 (14:54 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Sun, 23 Jun 2013 12:54:30 +0000 (14:54 +0200)
complex.__format__().

Lib/test/test_format.py
Misc/NEWS
Python/formatter_unicode.c

index f8f5420040be33bcfe3048a561c57489ba18a527..bd159f56893c5261c5c463f72021c2239f08a0be 100644 (file)
@@ -312,6 +312,23 @@ class FormatTest(unittest.TestCase):
 def test_main():
     support.run_unittest(FormatTest)
 
+    def test_precision(self):
+        INT_MAX = 2147483647
+
+        f = 1.2
+        self.assertEqual(format(f, ".0f"), "1")
+        self.assertEqual(format(f, ".3f"), "1.200")
+        with self.assertRaises(ValueError) as cm:
+            format(f, ".%sf" % (INT_MAX + 1))
+        self.assertEqual(str(cm.exception), "precision too big")
+
+        c = complex(f)
+        self.assertEqual(format(f, ".0f"), "1")
+        self.assertEqual(format(f, ".3f"), "1.200")
+        with self.assertRaises(ValueError) as cm:
+            format(f, ".%sf" % (INT_MAX + 1))
+        self.assertEqual(str(cm.exception), "precision too big")
+
 
 if __name__ == "__main__":
     unittest.main()
index 59e915c706423b2a75ddbc4764e6b46c295742ee..04b5e41ad6efd351d1431ef401a0de6b5f6eacfa 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 3.3.3 release candidate 1?
 Core and Builtins
 -----------------
 
+- Issue #18137: Detect integer overflow on precision in float.__format__()
+  and complex.__format__().
+
 - Issue #18183: Fix various unicode operations on strings with large unicode
   codepoints.
 
index 17eb9789e041220604bacf5b38da7484d80993f4..2fe446a1b0914194e73239faebbd04d124218ab8 100644 (file)
@@ -977,7 +977,7 @@ format_float_internal(PyObject *value,
     Py_ssize_t n_total;
     int has_decimal;
     double val;
-    Py_ssize_t precision = format->precision;
+    Py_ssize_t precision;
     Py_ssize_t default_precision = 6;
     Py_UCS4 type = format->type;
     int add_pct = 0;
@@ -994,6 +994,12 @@ format_float_internal(PyObject *value,
        from a hard-code pseudo-locale */
     LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
 
+    if (format->precision > INT_MAX) {
+        PyErr_SetString(PyExc_ValueError, "precision too big");
+        goto done;
+    }
+    precision = (int)format->precision;
+
     if (format->alternate)
         flags |= Py_DTSF_ALT;
 
@@ -1127,7 +1133,7 @@ format_complex_internal(PyObject *value,
     Py_ssize_t n_im_total;
     int re_has_decimal;
     int im_has_decimal;
-    Py_ssize_t precision = format->precision;
+    int precision;
     Py_ssize_t default_precision = 6;
     Py_UCS4 type = format->type;
     Py_ssize_t i_re;
@@ -1155,6 +1161,12 @@ format_complex_internal(PyObject *value,
        from a hard-code pseudo-locale */
     LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
 
+    if (format->precision > INT_MAX) {
+        PyErr_SetString(PyExc_ValueError, "precision too big");
+        goto done;
+    }
+    precision = (int)format->precision;
+
     /* Zero padding is not allowed. */
     if (format->fill_char == '0') {
         PyErr_SetString(PyExc_ValueError,