Mercurial > cpython-lru_cache-weakref
diff Objects/stringlib/string_format.h @ 80014:21fb1767e185 2.7
Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
| author | Mark Dickinson <mdickinson@enthought.com> |
|---|---|
| date | Sun, 28 Oct 2012 10:00:46 +0000 |
| parents | ee5350f1d0c2 |
| children |
line wrap: on
line diff
--- a/Objects/stringlib/string_format.h Sun Oct 28 10:51:35 2012 +0100 +++ b/Objects/stringlib/string_format.h Sun Oct 28 10:00:46 2012 +0000 @@ -197,7 +197,6 @@ { Py_ssize_t accumulator = 0; Py_ssize_t digitval; - Py_ssize_t oldaccumulator; STRINGLIB_CHAR *p; /* empty string is an error */ @@ -209,19 +208,17 @@ if (digitval < 0) return -1; /* - This trick was copied from old Unicode format code. It's cute, - but would really suck on an old machine with a slow divide - implementation. Fortunately, in the normal case we do not - expect too many digits. + Detect possible overflow before it happens: + + accumulator * 10 + digitval > PY_SSIZE_T_MAX if and only if + accumulator > (PY_SSIZE_T_MAX - digitval) / 10. */ - oldaccumulator = accumulator; - accumulator *= 10; - if ((accumulator+10)/10 != oldaccumulator+1) { + if (accumulator > (PY_SSIZE_T_MAX - digitval) / 10) { PyErr_Format(PyExc_ValueError, "Too many decimal digits in format string"); return -1; } - accumulator += digitval; + accumulator = accumulator * 10 + digitval; } return accumulator; }
