[Python-checkins] python/dist/src/Objects classobject.c, 2.176.2.1, 2.176.2.2 typeobject.c, 2.264.2.3, 2.264.2.4

gvanrossum@users.sourceforge.net gvanrossum at users.sourceforge.net
Tue Sep 20 18:36:11 CEST 2005


Update of /cvsroot/python/python/dist/src/Objects
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30524/Objects

Modified Files:
      Tag: release24-maint
	classobject.c typeobject.c 
Log Message:
- On 64-bit platforms, when __len__() returns a value that cannot be
  represented as a C int, raise OverflowError.
(Will forward-port.)


Index: classobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/classobject.c,v
retrieving revision 2.176.2.1
retrieving revision 2.176.2.2
diff -u -d -r2.176.2.1 -r2.176.2.2
--- classobject.c	31 Mar 2005 10:22:42 -0000	2.176.2.1
+++ classobject.c	20 Sep 2005 16:36:07 -0000	2.176.2.2
@@ -1013,7 +1013,17 @@
 	if (res == NULL)
 		return -1;
 	if (PyInt_Check(res)) {
-		outcome = PyInt_AsLong(res);
+		long temp = PyInt_AsLong(res);
+		outcome = (int)temp;
+#if SIZEOF_INT < SIZEOF_LONG
+		/* Overflow check -- range of PyInt is more than C int */
+		if (outcome != temp) {
+			PyErr_SetString(PyExc_OverflowError,
+			 "__len__() should return 0 <= outcome < 2**31");
+			outcome = -1;
+		}
+		else
+#endif
 		if (outcome < 0)
 			PyErr_SetString(PyExc_ValueError,
 					"__len__() should return >= 0");

Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.264.2.3
retrieving revision 2.264.2.4
diff -u -d -r2.264.2.3 -r2.264.2.4
--- typeobject.c	3 Jun 2005 14:13:47 -0000	2.264.2.3
+++ typeobject.c	20 Sep 2005 16:36:07 -0000	2.264.2.4
@@ -4071,14 +4071,24 @@
 {
 	static PyObject *len_str;
 	PyObject *res = call_method(self, "__len__", &len_str, "()");
+	long temp;
 	int len;
 
 	if (res == NULL)
 		return -1;
-	len = (int)PyInt_AsLong(res);
+	temp = PyInt_AsLong(res);
+	len = (int)temp;
 	Py_DECREF(res);
 	if (len == -1 && PyErr_Occurred())
 		return -1;
+#if SIZEOF_INT < SIZEOF_LONG
+	/* Overflow check -- range of PyInt is more than C int */
+	if (len != temp) {
+		PyErr_SetString(PyExc_OverflowError,
+			"__len__() should return 0 <= outcome < 2**31");
+		return -1;
+	}
+#endif
 	if (len < 0) {
 		PyErr_SetString(PyExc_ValueError,
 				"__len__() should return >= 0");



More information about the Python-checkins mailing list