summaryrefslogtreecommitdiff
path: root/Python
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-05-12 02:37:15 -0700
committerGitHub <noreply@github.com>2019-05-12 02:37:15 -0700
commitb7e483b6d07081d5f81860258e95785975a7cbf8 (patch)
treee13640130cdfe2dea53d1f4e7256bd02b9a49842 /Python
parent4f5febdf955240b6064bb5647dec084feaa98376 (diff)
downloadcpython-git-b7e483b6d07081d5f81860258e95785975a7cbf8.tar.gz
bpo-36791: Safer detection of integer overflow in sum(). (GH-13080)
(cherry picked from commit 29500737d45cbca9604d9ce845fb2acc3f531401) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Python')
-rw-r--r--Python/bltinmodule.c8
1 files changed, 5 insertions, 3 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 6306c3ac56..8083ac961f 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -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;
}