Unverified Kaydet (Commit) 29500737 authored tarafından Serhiy Storchaka's avatar Serhiy Storchaka Kaydeden (comit) GitHub

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

üst 88f07a80
...@@ -2375,9 +2375,11 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start) ...@@ -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;
} }
......
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