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.
........
diff --git a/Python/ceval.c b/Python/ceval.c
index abe157e..6c71c27 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -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);