mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
This commit is contained in:
parent
579d5cd643
commit
fb90c0934c
5 changed files with 45 additions and 20 deletions
|
@ -197,7 +197,6 @@ get_integer(const SubString *str)
|
|||
{
|
||||
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 @@ get_integer(const SubString *str)
|
|||
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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue