mirror of
https://github.com/python/cpython.git
synced 2025-12-04 00:30:19 +00:00
bpo-36791: Safer detection of integer overflow in sum(). (GH-13080)
This commit is contained in:
parent
88f07a804a
commit
29500737d4
1 changed files with 5 additions and 3 deletions
|
|
@ -2375,9 +2375,11 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
|
||||||
}
|
}
|
||||||
if (PyLong_CheckExact(item)) {
|
if (PyLong_CheckExact(item)) {
|
||||||
long b = PyLong_AsLongAndOverflow(item, &overflow);
|
long b = PyLong_AsLongAndOverflow(item, &overflow);
|
||||||
long x = i_result + b;
|
if (overflow == 0 &&
|
||||||
if (overflow == 0 && ((x^i_result) >= 0 || (x^b) >= 0)) {
|
(i_result >= 0 ? (b <= LONG_MAX - i_result)
|
||||||
i_result = x;
|
: (b >= LONG_MIN - i_result)))
|
||||||
|
{
|
||||||
|
i_result += b;
|
||||||
Py_DECREF(item);
|
Py_DECREF(item);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue