summaryrefslogtreecommitdiffstats
path: root/Objects/intobject.c
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2009-12-02 17:36:34 (GMT)
committerMark Dickinson <dickinsm@gmail.com>2009-12-02 17:36:34 (GMT)
commit6df863c93ad7e14fc163d4baae2762274511da48 (patch)
tree63bc90cfc0100e23faa07328560d92481f277f1d /Objects/intobject.c
parent0d124c29e2d801e81ccfa67767a6cb07f1b7756c (diff)
downloadcpython-6df863c93ad7e14fc163d4baae2762274511da48.zip
cpython-6df863c93ad7e14fc163d4baae2762274511da48.tar.gz
cpython-6df863c93ad7e14fc163d4baae2762274511da48.tar.bz2
Merged revisions 76629 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r76629 | mark.dickinson | 2009-12-02 17:33:41 +0000 (Wed, 02 Dec 2009) | 3 lines Issue #7406: Fix some occurrences of potential signed overflow in int arithmetic. ........
Diffstat (limited to 'Objects/intobject.c')
-rw-r--r--Objects/intobject.c9
1 files changed, 6 insertions, 3 deletions
diff --git a/Objects/intobject.c b/Objects/intobject.c
index e73c921..ebe029e 100644
--- a/Objects/intobject.c
+++ b/Objects/intobject.c
@@ -465,7 +465,8 @@ int_add(PyIntObject *v, PyIntObject *w)
register long a, b, x;
CONVERT_TO_LONG(v, a);
CONVERT_TO_LONG(w, b);
- x = a + b;
+ /* casts in the line below avoid undefined behaviour on overflow */
+ x = (long)((unsigned long)a + b);
if ((x^a) >= 0 || (x^b) >= 0)
return PyInt_FromLong(x);
return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
@@ -477,7 +478,8 @@ int_sub(PyIntObject *v, PyIntObject *w)
register long a, b, x;
CONVERT_TO_LONG(v, a);
CONVERT_TO_LONG(w, b);
- x = a - b;
+ /* casts in the line below avoid undefined behaviour on overflow */
+ x = (long)((unsigned long)a - b);
if ((x^a) >= 0 || (x^~b) >= 0)
return PyInt_FromLong(x);
return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
@@ -520,7 +522,8 @@ int_mul(PyObject *v, PyObject *w)
CONVERT_TO_LONG(v, a);
CONVERT_TO_LONG(w, b);
- longprod = a * b;
+ /* casts in the next line avoid undefined behaviour on overflow */
+ longprod = (long)((unsigned long)a * b);
doubleprod = (double)a * (double)b;
doubled_longprod = (double)longprod;