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

Serhiy Storchaka webhook-mailer at python.org
Sun May 5 07:26:28 EDT 2019


https://github.com/python/cpython/commit/29500737d45cbca9604d9ce845fb2acc3f531401
commit: 29500737d45cbca9604d9ce845fb2acc3f531401
branch: master
author: Serhiy Storchaka <storchaka at gmail.com>
committer: GitHub <noreply at github.com>
date: 2019-05-05T14:26:23+03:00
summary:

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

files:
M Python/bltinmodule.c

diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 7a2b259cbd89..047cca057b41 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -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;
                 }



More information about the Python-checkins mailing list