diff Python/ceval.c @ 57533:8f6eee42f810 legacy-trunk

Issue #7406: Fix some occurrences of potential signed overflow in int arithmetic.
author Mark Dickinson <dickinsm@gmail.com>
date Wed, 02 Dec 2009 17:33:41 +0000
parents f594313b9364
children c2fdf25329ff
line wrap: on
line diff
--- a/Python/ceval.c	Wed Dec 02 14:27:11 2009 +0000
+++ b/Python/ceval.c	Wed Dec 02 17:33:41 2009 +0000
@@ -1321,7 +1321,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);
@@ -1351,7 +1353,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);