summaryrefslogtreecommitdiffstats
path: root/Python/bltinmodule.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2019-05-05 11:26:23 (GMT)
committerGitHub <noreply@github.com>2019-05-05 11:26:23 (GMT)
commit29500737d45cbca9604d9ce845fb2acc3f531401 (patch)
tree5136598be241d81304a00d8018ecdd8f363482a9 /Python/bltinmodule.c
parent88f07a804a0adc0b6ee87687b59d8416113c7331 (diff)
downloadcpython-29500737d45cbca9604d9ce845fb2acc3f531401.zip
cpython-29500737d45cbca9604d9ce845fb2acc3f531401.tar.gz
cpython-29500737d45cbca9604d9ce845fb2acc3f531401.tar.bz2
bpo-36791: Safer detection of integer overflow in sum(). (GH-13080)
Diffstat (limited to 'Python/bltinmodule.c')
-rw-r--r--Python/bltinmodule.c8
1 files changed, 5 insertions, 3 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 7a2b259..047cca0 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;
}