mirror of
https://github.com/python/cpython.git
synced 2025-12-04 00:30:19 +00:00
Issue #1621: Fix undefined behaviour from signed overflow in datetime module hashes, array and list iterations, and get_integer (stringlib/string_format.h)
This commit is contained in:
parent
50203a69b3
commit
c7d93b7614
4 changed files with 14 additions and 16 deletions
|
|
@ -209,19 +209,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