]> granicus.if.org Git - python/commitdiff
Handle a whole lot of failures from PyString_FromInternedString().
authorNeal Norwitz <nnorwitz@gmail.com>
Sun, 13 Aug 2006 18:10:28 +0000 (18:10 +0000)
committerNeal Norwitz <nnorwitz@gmail.com>
Sun, 13 Aug 2006 18:10:28 +0000 (18:10 +0000)
Should fix most of Klocwork 234-272.

Objects/classobject.c

index e8bac91df435ba4c338ef68e3bbe1870fd0e309a..0e4356b4c8a7ebcd65f3bcc984a4bd348ff70c36 100644 (file)
@@ -103,8 +103,14 @@ PyClass_New(PyObject *bases, PyObject *dict, PyObject *name)
        op->cl_name = name;
        if (getattrstr == NULL) {
                getattrstr = PyString_InternFromString("__getattr__");
+               if (getattrstr == NULL)
+                       return NULL;
                setattrstr = PyString_InternFromString("__setattr__");
+               if (setattrstr == NULL)
+                       return NULL;
                delattrstr = PyString_InternFromString("__delattr__");
+               if (delattrstr == NULL)
+                       return NULL;
        }
        op->cl_getattr = class_lookup(op, getattrstr, &dummy);
        op->cl_setattr = class_lookup(op, setattrstr, &dummy);
@@ -522,11 +528,14 @@ PyInstance_New(PyObject *klass, PyObject *arg, PyObject *kw)
        PyObject *init;
        static PyObject *initstr;
 
+       if (initstr == NULL) {
+               initstr = PyString_InternFromString("__init__");
+               if (initstr == NULL)
+                       return NULL;
+       }
        inst = (PyInstanceObject *) PyInstance_NewRaw(klass, NULL);
        if (inst == NULL)
                return NULL;
-       if (initstr == NULL)
-               initstr = PyString_InternFromString("__init__");
        init = instance_getattr2(inst, initstr);
        if (init == NULL) {
                if (PyErr_Occurred()) {
@@ -612,8 +621,11 @@ instance_dealloc(register PyInstanceObject *inst)
        /* Save the current exception, if any. */
        PyErr_Fetch(&error_type, &error_value, &error_traceback);
        /* Execute __del__ method, if any. */
-       if (delstr == NULL)
+       if (delstr == NULL) {
                delstr = PyString_InternFromString("__del__");
+               if (delstr == NULL)
+                       return NULL;
+       }
        if ((del = instance_getattr2(inst, delstr)) != NULL) {
                PyObject *res = PyEval_CallObject(del, (PyObject *)NULL);
                if (res == NULL)
@@ -840,8 +852,11 @@ instance_repr(PyInstanceObject *inst)
        PyObject *res;
        static PyObject *reprstr;
 
-       if (reprstr == NULL)
+       if (reprstr == NULL) {
                reprstr = PyString_InternFromString("__repr__");
+               if (reprstr == NULL)
+                       return NULL;
+       }
        func = instance_getattr(inst, reprstr);
        if (func == NULL) {
                PyObject *classname, *mod;
@@ -876,8 +891,11 @@ instance_str(PyInstanceObject *inst)
        PyObject *res;
        static PyObject *strstr;
 
-       if (strstr == NULL)
+       if (strstr == NULL) {
                strstr = PyString_InternFromString("__str__");
+               if (strstr == NULL)
+                       return NULL;
+       }
        func = instance_getattr(inst, strstr);
        if (func == NULL) {
                if (!PyErr_ExceptionMatches(PyExc_AttributeError))
@@ -898,8 +916,11 @@ instance_hash(PyInstanceObject *inst)
        long outcome;
        static PyObject *hashstr, *eqstr, *cmpstr;
 
-       if (hashstr == NULL)
+       if (hashstr == NULL) {
                hashstr = PyString_InternFromString("__hash__");
+               if (hashstr == NULL)
+                       return -1;
+       }
        func = instance_getattr(inst, hashstr);
        if (func == NULL) {
                if (!PyErr_ExceptionMatches(PyExc_AttributeError))
@@ -908,15 +929,21 @@ instance_hash(PyInstanceObject *inst)
                /* If there is no __eq__ and no __cmp__ method, we hash on the
                   address.  If an __eq__ or __cmp__ method exists, there must
                   be a __hash__. */
-               if (eqstr == NULL)
+               if (eqstr == NULL) {
                        eqstr = PyString_InternFromString("__eq__");
+                       if (eqstr == NULL)
+                               return -1;
+               }
                func = instance_getattr(inst, eqstr);
                if (func == NULL) {
                        if (!PyErr_ExceptionMatches(PyExc_AttributeError))
                                return -1;
                        PyErr_Clear();
-                       if (cmpstr == NULL)
+                       if (cmpstr == NULL) {
                                cmpstr = PyString_InternFromString("__cmp__");
+                               if (cmpstr == NULL)
+                                       return -1;
+                       }
                        func = instance_getattr(inst, cmpstr);
                        if (func == NULL) {
                                if (!PyErr_ExceptionMatches(
@@ -964,8 +991,11 @@ instance_length(PyInstanceObject *inst)
        PyObject *res;
        Py_ssize_t outcome;
 
-       if (lenstr == NULL)
+       if (lenstr == NULL) {
                lenstr = PyString_InternFromString("__len__");
+               if (lenstr == NULL)
+                       return -1;
+       }
        func = instance_getattr(inst, lenstr);
        if (func == NULL)
                return -1;
@@ -1010,8 +1040,11 @@ instance_subscript(PyInstanceObject *inst, PyObject *key)
        PyObject *arg;
        PyObject *res;
 
-       if (getitemstr == NULL)
+       if (getitemstr == NULL) {
                getitemstr = PyString_InternFromString("__getitem__");
+               if (getitemstr == NULL)
+                       return NULL;
+       }
        func = instance_getattr(inst, getitemstr);
        if (func == NULL)
                return NULL;
@@ -1034,13 +1067,19 @@ instance_ass_subscript(PyInstanceObject *inst, PyObject *key, PyObject *value)
        PyObject *res;
 
        if (value == NULL) {
-               if (delitemstr == NULL)
+               if (delitemstr == NULL) {
                        delitemstr = PyString_InternFromString("__delitem__");
+                       if (delitemstr == NULL)
+                               return -1;
+               }
                func = instance_getattr(inst, delitemstr);
        }
        else {
-               if (setitemstr == NULL)
+               if (setitemstr == NULL) {
                        setitemstr = PyString_InternFromString("__setitem__");
+                       if (setitemstr == NULL)
+                               return -1;
+               }
                func = instance_getattr(inst, setitemstr);
        }
        if (func == NULL)
@@ -1073,8 +1112,11 @@ instance_item(PyInstanceObject *inst, Py_ssize_t i)
 {
        PyObject *func, *res;
 
-       if (getitemstr == NULL)
+       if (getitemstr == NULL) {
                getitemstr = PyString_InternFromString("__getitem__");
+               if (getitemstr == NULL)
+                       return NULL;
+       }
        func = instance_getattr(inst, getitemstr);
        if (func == NULL)
                return NULL;
@@ -1089,8 +1131,11 @@ instance_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j)
        PyObject *func, *arg, *res;
        static PyObject *getslicestr;
 
-       if (getslicestr == NULL)
+       if (getslicestr == NULL) {
                getslicestr = PyString_InternFromString("__getslice__");
+               if (getslicestr == NULL)
+                       return NULL;
+       }
        func = instance_getattr(inst, getslicestr);
 
        if (func == NULL) {
@@ -1098,8 +1143,11 @@ instance_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j)
                        return NULL;
                PyErr_Clear();
 
-               if (getitemstr == NULL)
+               if (getitemstr == NULL) {
                        getitemstr = PyString_InternFromString("__getitem__");
+                       if (getitemstr == NULL)
+                               return NULL;
+               }
                func = instance_getattr(inst, getitemstr);
                if (func == NULL)
                        return NULL;
@@ -1123,13 +1171,19 @@ instance_ass_item(PyInstanceObject *inst, Py_ssize_t i, PyObject *item)
        PyObject *func, *arg, *res;
 
        if (item == NULL) {
-               if (delitemstr == NULL)
+               if (delitemstr == NULL) {
                        delitemstr = PyString_InternFromString("__delitem__");
+                       if (delitemstr == NULL)
+                               return -1;
+               }
                func = instance_getattr(inst, delitemstr);
        }
        else {
-               if (setitemstr == NULL)
+               if (setitemstr == NULL) {
                        setitemstr = PyString_InternFromString("__setitem__");
+                       if (setitemstr == NULL)
+                               return -1;
+               }
                func = instance_getattr(inst, setitemstr);
        }
        if (func == NULL)
@@ -1158,17 +1212,23 @@ instance_ass_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j, PyObject
        static PyObject *setslicestr, *delslicestr;
 
        if (value == NULL) {
-               if (delslicestr == NULL)
+               if (delslicestr == NULL) {
                        delslicestr =
                                PyString_InternFromString("__delslice__");
+                       if (delslicestr == NULL)
+                               return -1;
+               }
                func = instance_getattr(inst, delslicestr);
                if (func == NULL) {
                        if (!PyErr_ExceptionMatches(PyExc_AttributeError))
                                return -1;
                        PyErr_Clear();
-                       if (delitemstr == NULL)
+                       if (delitemstr == NULL) {
                                delitemstr =
                                    PyString_InternFromString("__delitem__");
+                               if (delitemstr == NULL)
+                                       return -1;
+                       }
                        func = instance_getattr(inst, delitemstr);
                        if (func == NULL)
                                return -1;
@@ -1179,17 +1239,23 @@ instance_ass_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j, PyObject
                        arg = Py_BuildValue("(nn)", i, j);
        }
        else {
-               if (setslicestr == NULL)
+               if (setslicestr == NULL) {
                        setslicestr =
                                PyString_InternFromString("__setslice__");
+                       if (setslicestr == NULL)
+                               return -1;
+               }
                func = instance_getattr(inst, setslicestr);
                if (func == NULL) {
                        if (!PyErr_ExceptionMatches(PyExc_AttributeError))
                                return -1;
                        PyErr_Clear();
-                       if (setitemstr == NULL)
+                       if (setitemstr == NULL) {
                                setitemstr =
                                    PyString_InternFromString("__setitem__");
+                               if (setitemstr == NULL)
+                                       return -1;
+                       }
                        func = instance_getattr(inst, setitemstr);
                        if (func == NULL)
                                return -1;
@@ -1462,7 +1528,8 @@ instance_coerce(PyObject **pv, PyObject **pw)
 #define UNARY(funcname, methodname) \
 static PyObject *funcname(PyInstanceObject *self) { \
        static PyObject *o; \
-       if (o == NULL) o = PyString_InternFromString(methodname); \
+       if (o == NULL) { o = PyString_InternFromString(methodname); \
+                        if (o == NULL) return NULL; } \
        return generic_unary_op(self, o); \
 }
 
@@ -1634,14 +1701,20 @@ instance_nonzero(PyInstanceObject *self)
        long outcome;
        static PyObject *nonzerostr;
 
-       if (nonzerostr == NULL)
+       if (nonzerostr == NULL) {
                nonzerostr = PyString_InternFromString("__nonzero__");
+               if (nonzerostr == NULL)
+                       return -1;
+       }
        if ((func = instance_getattr(self, nonzerostr)) == NULL) {
                if (!PyErr_ExceptionMatches(PyExc_AttributeError))
                        return -1;
                PyErr_Clear();
-               if (lenstr == NULL)
+               if (lenstr == NULL) {
                        lenstr = PyString_InternFromString("__len__");
+                       if (lenstr == NULL)
+                               return -1;
+               }
                if ((func = instance_getattr(self, lenstr)) == NULL) {
                        if (!PyErr_ExceptionMatches(PyExc_AttributeError))
                                return -1;
@@ -1923,8 +1996,11 @@ instance_iternext(PyInstanceObject *self)
 {
        PyObject *func;
 
-       if (nextstr == NULL)
+       if (nextstr == NULL) {
                nextstr = PyString_InternFromString("next");
+               if (nextstr == NULL)
+                       return NULL;
+       }
 
        if ((func = instance_getattr(self, nextstr)) != NULL) {
                PyObject *res = PyEval_CallObject(func, (PyObject *)NULL);