cpython: ef5175d08e7e (original) (raw)
Mercurial > cpython
changeset 84266:ef5175d08e7e 3.3
Issue #18137: Detect integer overflow on precision in float.__format__() and complex.__format__(). [#18137]
Victor Stinner victor.stinner@gmail.com | |
---|---|
date | Sun, 23 Jun 2013 14:54:30 +0200 |
parents | 7ecca1a98220 |
children | 81fef2666ebb 1bd49f8a30da |
files | Lib/test/test_format.py Misc/NEWS Python/formatter_unicode.c |
diffstat | 3 files changed, 34 insertions(+), 2 deletions(-)[+] [-] Lib/test/test_format.py 17 Misc/NEWS 3 Python/formatter_unicode.c 16 |
line wrap: on
line diff
--- a/Lib/test/test_format.py +++ b/Lib/test/test_format.py @@ -312,6 +312,23 @@ class FormatTest(unittest.TestCase): def test_main(): support.run_unittest(FormatTest)
f = 1.2[](#l1.10)
self.assertEqual(format(f, ".0f"), "1")[](#l1.11)
self.assertEqual(format(f, ".3f"), "1.200")[](#l1.12)
with self.assertRaises(ValueError) as cm:[](#l1.13)
format(f, ".%sf" % (INT_MAX + 1))[](#l1.14)
self.assertEqual(str(cm.exception), "precision too big")[](#l1.15)
c = complex(f)[](#l1.17)
self.assertEqual(format(f, ".0f"), "1")[](#l1.18)
self.assertEqual(format(f, ".3f"), "1.200")[](#l1.19)
with self.assertRaises(ValueError) as cm:[](#l1.20)
format(f, ".%sf" % (INT_MAX + 1))[](#l1.21)
self.assertEqual(str(cm.exception), "precision too big")[](#l1.22)
+ if name == "main": unittest.main()
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -12,6 +12,9 @@ What's New in Python 3.3.3 release candi Core and Builtins ----------------- +- Issue #18137: Detect integer overflow on precision in float.format()
--- a/Python/formatter_unicode.c +++ b/Python/formatter_unicode.c @@ -977,7 +977,7 @@ format_float_internal(PyObject *value, Py_ssize_t n_total; int has_decimal; double val;
- 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");[](#l3.17)
goto done;[](#l3.18)
- }
- 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;
- 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");[](#l3.39)
goto done;[](#l3.40)
- }
- precision = (int)format->precision;
+ /* Zero padding is not allowed. */ if (format->fill_char == '0') { PyErr_SetString(PyExc_ValueError,