]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
- On 64-bit platforms, when __len__() returns a value that cannot be
authorGuido van Rossum <guido@python.org>
Tue, 20 Sep 2005 16:36:07 +0000 (16:36 +0000)
committerGuido van Rossum <guido@python.org>
Tue, 20 Sep 2005 16:36:07 +0000 (16:36 +0000)
  represented as a C int, raise OverflowError.
(Will forward-port.)

Misc/NEWS
Objects/classobject.c
Objects/typeobject.c

index c3689372a2c523e5a060b91a6fa875e970e2c248..b77e6dbdacebbfb7fb9d2877e42a69ca75b2a45e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.4.2c1
 Core and builtins
 -----------------
 
+- On 64-bit platforms, when __len__() returns a value that cannot be
+  represented as a C int, raise OverflowError.
+
 - SF bug #893549: parsing keyword arguments was broken with a few format
   codes.
 
index 68505f1527bc517d3f7ab920bfb5cf623f3ea370..187f8d0648c12b77e896f9971bf1721da5e287d9 100644 (file)
@@ -1013,7 +1013,17 @@ instance_length(PyInstanceObject *inst)
        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 391140c4c290b8ba45b07cc76b4f3f41291bc5c1..41c660d67e1c52a8c8309126c7fca343399bd281 100644 (file)
@@ -4071,14 +4071,24 @@ slot_sq_length(PyObject *self)
 {
        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");