]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-36791: Safer detection of integer overflow in sum(). (GH-13080)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 12 May 2019 09:37:15 +0000 (02:37 -0700)
committerGitHub <noreply@github.com>
Sun, 12 May 2019 09:37:15 +0000 (02:37 -0700)
(cherry picked from commit 29500737d45cbca9604d9ce845fb2acc3f531401)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Python/bltinmodule.c

index 6306c3ac56415c417b53083c71a1fbd85b4a2af4..8083ac961feb552173bb7dc2038b805708259b37 100644 (file)
@@ -2374,9 +2374,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;
                 }