Commit 29500737 authored by Serhiy Storchaka's avatar Serhiy Storchaka Committed by GitHub

bpo-36791: Safer detection of integer overflow in sum(). (GH-13080)

parent 88f07a80
......@@ -2375,9 +2375,11 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
}
if (PyLong_CheckExact(item)) {
long b = PyLong_AsLongAndOverflow(item, &overflow);
long x = i_result + b;
if (overflow == 0 && ((x^i_result) >= 0 || (x^b) >= 0)) {
i_result = x;
if (overflow == 0 &&
(i_result >= 0 ? (b <= LONG_MAX - i_result)
: (b >= LONG_MIN - i_result)))
{
i_result += b;
Py_DECREF(item);
continue;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment