[Python-checkins] r76630 - in python/branches/release26-maint: Objects/intobject.c Python/ceval.c
mark.dickinson
python-checkins at python.org
Wed Dec 2 18:36:34 CET 2009
Author: mark.dickinson
Date: Wed Dec 2 18:36:34 2009
New Revision: 76630
Log:
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.
........
Modified:
python/branches/release26-maint/ (props changed)
python/branches/release26-maint/Objects/intobject.c
python/branches/release26-maint/Python/ceval.c
Modified: python/branches/release26-maint/Objects/intobject.c
==============================================================================
--- python/branches/release26-maint/Objects/intobject.c (original)
+++ python/branches/release26-maint/Objects/intobject.c Wed Dec 2 18:36:34 2009
@@ -465,7 +465,8 @@
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 @@
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 @@
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;
Modified: python/branches/release26-maint/Python/ceval.c
==============================================================================
--- python/branches/release26-maint/Python/ceval.c (original)
+++ python/branches/release26-maint/Python/ceval.c Wed Dec 2 18:36:34 2009
@@ -1191,7 +1191,9 @@
register long a, b, i;
a = PyInt_AS_LONG(v);
b = PyInt_AS_LONG(w);
- i = a + b;
+ /* cast to avoid undefined behaviour
+ on overflow */
+ i = (long)((unsigned long)a + b);
if ((i^a) < 0 && (i^b) < 0)
goto slow_add;
x = PyInt_FromLong(i);
@@ -1221,7 +1223,9 @@
register long a, b, i;
a = PyInt_AS_LONG(v);
b = PyInt_AS_LONG(w);
- i = a - b;
+ /* cast to avoid undefined behaviour
+ on overflow */
+ i = (long)((unsigned long)a - b);
if ((i^a) < 0 && (i^~b) < 0)
goto slow_sub;
x = PyInt_FromLong(i);
More information about the Python-checkins
mailing list