]> granicus.if.org Git - python/commitdiff
Change int to Py_ssize_t in several places.
authorMartin v. Löwis <martin@v.loewis.de>
Tue, 7 Mar 2006 12:08:51 +0000 (12:08 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Tue, 7 Mar 2006 12:08:51 +0000 (12:08 +0000)
Add (int) casts to silence compiler warnings.
Raise Python exceptions for overflows.

Modules/posixmodule.c
Objects/enumobject.c
Objects/genobject.c
Objects/object.c
Objects/stringobject.c
Objects/typeobject.c
Python/bltinmodule.c
Python/exceptions.c
Python/import.c
Python/marshal.c
Python/sysmodule.c

index 6949c901b770c605b2990ec32d3cad2ca9f468f7..ba1e6c0ad577b3f8ad0c8e96b93b27dc521052b5 100644 (file)
@@ -2317,9 +2317,9 @@ posix__exit(PyObject *self, PyObject *args)
 
 #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
 static void
-free_string_array(char **array, int count)
+free_string_array(char **array, Py_ssize_t count)
 {
-       int i;
+       Py_ssize_t i;
        for (i = 0; i < count; i++)
                PyMem_Free(array[i]);
        PyMem_DEL(array);
@@ -2341,7 +2341,7 @@ posix_execv(PyObject *self, PyObject *args)
        char *path;
        PyObject *argv;
        char **argvlist;
-       int i, argc;
+       Py_ssize_t i, argc;
        PyObject *(*getitem)(PyObject *, Py_ssize_t);
 
        /* execv has two arguments: (path, argv), where
@@ -2410,7 +2410,7 @@ posix_execve(PyObject *self, PyObject *args)
        char **argvlist;
        char **envlist;
        PyObject *key, *val, *keys=NULL, *vals=NULL;
-       int i, pos, argc, envc;
+       Py_ssize_t i, pos, argc, envc;
        PyObject *(*getitem)(PyObject *, Py_ssize_t);
        int lastarg = 0;
 
index 5999ae9bbdea291e6ea8dbff3c8b9fbc49ba1250..481123920f2bbb0423618d259e65237f678af0ad 100644 (file)
@@ -216,7 +216,7 @@ static PyObject *
 reversed_next(reversedobject *ro)
 {
        PyObject *item;
-       long index = ro->index;
+       Py_ssize_t index = ro->index;
 
        if (index >= 0) {
                item = PySequence_GetItem(ro->seq, index);
index c24c3ce7ab5565db5a3791cb0d4d6fcd2ed02b8d..3f6ef859eedbbe2704bdbf0cbbfd5b1b07be826c 100644 (file)
@@ -177,7 +177,7 @@ gen_del(PyObject *self)
          * never happened.
          */
         {
-                int refcnt = self->ob_refcnt;
+                Py_ssize_t refcnt = self->ob_refcnt;
                 _Py_NewReference(self);
                 self->ob_refcnt = refcnt;
         }
index a69a0ad51d6e297406a63f768803e3967db23439..866ce068c2091db5c4208dbe6464ca45a78ea8fc 100644 (file)
@@ -1172,7 +1172,7 @@ PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
 PyObject **
 _PyObject_GetDictPtr(PyObject *obj)
 {
-       long dictoffset;
+       Py_ssize_t dictoffset;
        PyTypeObject *tp = obj->ob_type;
 
        if (!(tp->tp_flags & Py_TPFLAGS_HAVE_CLASS))
@@ -1212,7 +1212,7 @@ PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
        PyObject *descr = NULL;
        PyObject *res = NULL;
        descrgetfunc f;
-       long dictoffset;
+       Py_ssize_t dictoffset;
        PyObject **dictptr;
 
        if (!PyString_Check(name)){
index 4fe24a345778ed6941fb32669eb3f2ea5af08538..0030ac7193f18e922e8e478fd4b8f512072b2516 100644 (file)
@@ -3679,6 +3679,7 @@ _PyString_FormatLong(PyObject *val, int flags, int prec, int type,
        Py_ssize_t i;
        int sign;       /* 1 if '-', else 0 */
        int len;        /* number of characters */
+       Py_ssize_t llen;
        int numdigits;  /* len == numnondigits + numdigits */
        int numnondigits = 0;
 
@@ -3707,7 +3708,12 @@ _PyString_FormatLong(PyObject *val, int flags, int prec, int type,
                return NULL;
        }
        buf = PyString_AsString(result);
-       len = PyString_Size(result);
+       llen = PyString_Size(result);
+       if (llen > INT_MAX) {
+               PyErr_SetString(PyExc_ValueError, "string too large in _PyString_FormatLong");
+               return NULL;
+       }
+       len = (int)llen;
        if (buf[len-1] == 'L') {
                --len;
                buf[len] = '\0';
@@ -3941,12 +3947,12 @@ PyString_Format(PyObject *format, PyObject *args)
                        PyObject *temp = NULL;
                        char *pbuf;
                        int sign;
-                       int len;
+                       Py_ssize_t len;
                        char formatbuf[FORMATBUFLEN];
                             /* For format{float,int,char}() */
 #ifdef Py_USING_UNICODE
                        char *fmt_start = fmt;
-                       int argidx_start = argidx;
+                       Py_ssize_t argidx_start = argidx;
 #endif
 
                        fmt++;
@@ -4139,8 +4145,10 @@ PyString_Format(PyObject *format, PyObject *args)
                                if (c == 'i')
                                        c = 'd';
                                if (PyLong_Check(v)) {
+                                       int ilen;
                                        temp = _PyString_FormatLong(v, flags,
-                                               prec, c, &pbuf, &len);
+                                               prec, c, &pbuf, &ilen);
+                                       len = ilen;
                                        if (!temp)
                                                goto error;
                                        sign = 1;
index 6072c08687940b4b00552ea856f05d8418b30127..9837e38e429000a0aa0c432e57e46c65cfb3cd20 100644 (file)
@@ -4244,7 +4244,8 @@ slot_sq_contains(PyObject *self, PyObject *value)
                }
        }
        else if (! PyErr_Occurred()) {
-               result = _PySequence_IterSearch(self, value,
+               /* Possible results: -1 and 1 */
+               result = (int)_PySequence_IterSearch(self, value,
                                                 PY_ITERSEARCH_CONTAINS);
        }
        return result;
@@ -4880,7 +4881,7 @@ slot_tp_del(PyObject *self)
         * never happened.
         */
        {
-               int refcnt = self->ob_refcnt;
+               Py_ssize_t refcnt = self->ob_refcnt;
                _Py_NewReference(self);
                self->ob_refcnt = refcnt;
        }
index 409afd854407447edefa6077c0ef71edc78a71e8..c9da78c6f6f1fe2cfb8f1bef7c31c65391d8eedc 100644 (file)
@@ -2514,7 +2514,7 @@ static PyObject *
 filterunicode(PyObject *func, PyObject *strobj)
 {
        PyObject *result;
-       register int i, j;
+       register Py_ssize_t i, j;
        Py_ssize_t len = PyUnicode_GetSize(strobj);
        Py_ssize_t outlen = len;
 
index 5dec197bd86c435d7339e318517bd9413f20d9e5..b146c971b9c87a4c77f9dc261ea19c5fa6288233 100644 (file)
@@ -764,7 +764,7 @@ static PyObject *
 SyntaxError__init__(PyObject *self, PyObject *args)
 {
     PyObject *rtnval = NULL;
-    int lenargs;
+    Py_ssize_t lenargs;
 
     if (!(self = get_self(args)))
        return NULL;
@@ -889,7 +889,7 @@ SyntaxError__str__(PyObject *self, PyObject *args)
            PyErr_Clear();
 
        if (have_filename || have_lineno) {
-           int bufsize = PyString_GET_SIZE(str) + 64;
+           Py_ssize_t bufsize = PyString_GET_SIZE(str) + 64;
            if (have_filename)
                bufsize += PyString_GET_SIZE(filename);
 
index 4aeeb3a8b7fa8d570fff1c1f286a52c60d6bb60d..f214ed5cd2adca9dfe8858b62d2c1f44c3676b99 100644 (file)
@@ -853,7 +853,7 @@ write_compiled_module(PyCodeObject *co, char *cpathname, time_t mtime)
        /* Now write the true mtime */
        fseek(fp, 4L, 0);
        assert(mtime < LONG_MAX);
-       PyMarshal_WriteLongToFile(mtime, fp, Py_MARSHAL_VERSION);
+       PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
        fflush(fp);
        fclose(fp);
        if (Py_VerboseFlag)
@@ -1016,7 +1016,7 @@ get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
                  PyObject *p)
 {
        PyObject *importer;
-       int j, nhooks;
+       Py_ssize_t j, nhooks;
 
        /* These conditions are the caller's responsibility: */
        assert(PyList_Check(path_hooks));
@@ -1075,7 +1075,7 @@ static struct filedescr *
 find_module(char *fullname, char *subname, PyObject *path, char *buf,
            size_t buflen, FILE **p_fp, PyObject **p_loader)
 {
-       int i, npath;
+       Py_ssize_t i, npath;
        size_t len, namelen;
        struct filedescr *fdp = NULL;
        char *filemode;
@@ -2028,7 +2028,7 @@ get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level)
 
        modpath = PyDict_GetItem(globals, pathstr);
        if (modpath != NULL) {
-               int len = PyString_GET_SIZE(modname);
+               Py_ssize_t len = PyString_GET_SIZE(modname);
                if (len > MAXPATHLEN) {
                        PyErr_SetString(PyExc_ValueError,
                                        "Module name too long");
index 7234a5cc9525fdd1d2629d5ce4e84f08ebdb433a..c5d5b72f7dfdc8b51d84b7aad4cf7ef3e0aea11a 100644 (file)
@@ -186,7 +186,7 @@ w_object(PyObject *v, WFILE *p)
                        n = strlen(buf);
                        w_byte(TYPE_FLOAT, p);
                        w_byte((int)n, p);
-                       w_string(buf, n, p);
+                       w_string(buf, (int)n, p);
                }
        }
 #ifndef WITHOUT_COMPLEX
@@ -215,16 +215,16 @@ w_object(PyObject *v, WFILE *p)
                                PyComplex_RealAsDouble(v));
                        PyFloat_AsReprString(buf, temp);
                        Py_DECREF(temp);
-                       n = (int)strlen(buf);
-                       w_byte(n, p);
-                       w_string(buf, n, p);
+                       n = strlen(buf);
+                       w_byte((int)n, p);
+                       w_string(buf, (int)n, p);
                        temp = (PyFloatObject*)PyFloat_FromDouble(
                                PyComplex_ImagAsDouble(v));
                        PyFloat_AsReprString(buf, temp);
                        Py_DECREF(temp);
-                       n = (int)strlen(buf);
-                       w_byte(n, p);
-                       w_string(buf, n, p);
+                       n = strlen(buf);
+                       w_byte((int)n, p);
+                       w_string(buf, (int)n, p);
                }
        }
 #endif
@@ -248,8 +248,14 @@ w_object(PyObject *v, WFILE *p)
                        w_byte(TYPE_STRING, p);
                }
                n = PyString_GET_SIZE(v);
+               if (n > INT_MAX) {
+                       /* huge strings are not supported */
+                       p->depth--;
+                       p->error = 1;
+                       return;
+               }
                w_long((long)n, p);
-               w_string(PyString_AS_STRING(v), n, p);
+               w_string(PyString_AS_STRING(v), (int)n, p);
        }
 #ifdef Py_USING_UNICODE
        else if (PyUnicode_Check(v)) {
@@ -262,8 +268,13 @@ w_object(PyObject *v, WFILE *p)
                }
                w_byte(TYPE_UNICODE, p);
                n = PyString_GET_SIZE(utf8);
+               if (n > INT_MAX) {
+                       p->depth--;
+                       p->error = 1;
+                       return;
+               }
                w_long((long)n, p);
-               w_string(PyString_AS_STRING(utf8), n, p);
+               w_string(PyString_AS_STRING(utf8), (int)n, p);
                Py_DECREF(utf8);
        }
 #endif
@@ -350,8 +361,13 @@ w_object(PyObject *v, WFILE *p)
                PyBufferProcs *pb = v->ob_type->tp_as_buffer;
                w_byte(TYPE_STRING, p);
                n = (*pb->bf_getreadbuffer)(v, 0, (void **)&s);
+               if (n > INT_MAX) {
+                       p->depth--;
+                       p->error = 1;
+                       return;
+               }
                w_long((long)n, p);
-               w_string(s, n, p);
+               w_string(s, (int)n, p);
        }
        else {
                w_byte(TYPE_UNKNOWN, p);
index a33ac26c6100b9b153ecd0f28e1a265cf2b8273b..6eadd06d9c3d6f1ee692069ffec9885e66052958 100644 (file)
@@ -597,7 +597,7 @@ sys_mdebug(PyObject *self, PyObject *args)
 static PyObject *
 sys_getrefcount(PyObject *self, PyObject *arg)
 {
-       return PyInt_FromLong(arg->ob_refcnt);
+       return PyInt_FromSsize_t(arg->ob_refcnt);
 }
 
 #ifdef Py_REF_DEBUG