bpo-35454: Fix miscellaneous minor issues in error handling. (#11077)
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 11 Dec 2018 06:38:03 +0000 (08:38 +0200)
committerGitHub <noreply@github.com>
Tue, 11 Dec 2018 06:38:03 +0000 (08:38 +0200)
* bpo-35454: Fix miscellaneous minor issues in error handling.

* Fix a null pointer dereference.

Modules/_elementtree.c
Modules/_threadmodule.c
Modules/socketmodule.c
Objects/namespaceobject.c
Python/_warnings.c
Python/ceval.c
Python/codecs.c
Python/import.c
Python/pylifecycle.c

index 62374d887c76e15fecb6f3973c19fb3fa28cd453..12e418d85ed5d6dacd4d013683229fca725fa094 100644 (file)
@@ -352,7 +352,10 @@ get_attrib_from_keywords(PyObject *kwds)
             return NULL;
         }
         attrib = PyDict_Copy(attrib);
-        PyDict_DelItem(kwds, attrib_str);
+        if (attrib && PyDict_DelItem(kwds, attrib_str) < 0) {
+            Py_DECREF(attrib);
+            attrib = NULL;
+        }
     } else {
         attrib = PyDict_New();
     }
index d075ef7455dbb9eab64c89bf802d778d6555e54a..aacce698e4a4db9ad20a46289a0c599cfade0144 100644 (file)
@@ -777,9 +777,11 @@ local_clear(localobject *self)
         for(tstate = PyInterpreterState_ThreadHead(tstate->interp);
             tstate;
             tstate = PyThreadState_Next(tstate))
-            if (tstate->dict &&
-                PyDict_GetItem(tstate->dict, self->key))
-                PyDict_DelItem(tstate->dict, self->key);
+            if (tstate->dict && PyDict_GetItem(tstate->dict, self->key)) {
+                if (PyDict_DelItem(tstate->dict, self->key)) {
+                    PyErr_Clear();
+                }
+            }
     }
     return 0;
 }
index 40f1ca64a4ac312f07343a2115d3b01158e1a0fa..73d3e1add3ef62dcea884ebfa99c8cbaafa39a97 100644 (file)
@@ -362,10 +362,14 @@ remove_unusable_flags(PyObject *m)
         else {
             if (PyDict_GetItemString(
                     dict,
-                    win_runtime_flags[i].flag_name) != NULL) {
-                PyDict_DelItemString(
-                    dict,
-                    win_runtime_flags[i].flag_name);
+                    win_runtime_flags[i].flag_name) != NULL)
+            {
+                if (PyDict_DelItemString(
+                        dict,
+                        win_runtime_flags[i].flag_name))
+                {
+                    PyErr_Clear();
+                }
             }
         }
     }
index 0b902693849dbb2b0107f26f67fc50966380fd5a..2acf809959da728244cd97a790b56ad4a480ebc0 100644 (file)
@@ -103,15 +103,15 @@ namespace_repr(PyObject *ns)
             PyObject *value, *item;
 
             value = PyDict_GetItem(d, key);
-            assert(value != NULL);
-
-            item = PyUnicode_FromFormat("%S=%R", key, value);
-            if (item == NULL) {
-                loop_error = 1;
-            }
-            else {
-                loop_error = PyList_Append(pairs, item);
-                Py_DECREF(item);
+            if (value != NULL) {
+                item = PyUnicode_FromFormat("%S=%R", key, value);
+                if (item == NULL) {
+                    loop_error = 1;
+                }
+                else {
+                    loop_error = PyList_Append(pairs, item);
+                    Py_DECREF(item);
+                }
             }
         }
 
index ccbc73f54e4ffab51038fad935f41cb0f66abfcd..7eedd1374cbacf5ddd621f4fb46f0b7909e2c6d7 100644 (file)
@@ -255,7 +255,11 @@ already_warned(PyObject *registry, PyObject *key, int should_set)
     version_obj = _PyDict_GetItemId(registry, &PyId_version);
     if (version_obj == NULL
         || !PyLong_CheckExact(version_obj)
-        || PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version) {
+        || PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version)
+    {
+        if (PyErr_Occurred()) {
+            return -1;
+        }
         PyDict_Clear(registry);
         version_obj = PyLong_FromLong(_PyRuntime.warnings.filters_version);
         if (version_obj == NULL)
index ebe1c50e7bad235d9b514a3100ab2dd2629bfc38..de6ff29945500ee4717af917a2bf6ddbefd3204d 100644 (file)
@@ -5109,7 +5109,7 @@ unicode_concatenate(PyObject *v, PyObject *w,
             PyObject *names = f->f_code->co_names;
             PyObject *name = GETITEM(names, oparg);
             PyObject *locals = f->f_locals;
-            if (PyDict_CheckExact(locals) &&
+            if (locals && PyDict_CheckExact(locals) &&
                 PyDict_GetItem(locals, name) == v) {
                 if (PyDict_DelItem(locals, name) != 0) {
                     PyErr_Clear();
index 002fad3080388ed78feec9b2bfe1ced2e2a286f3..ff2142df40ccbb745d8d69846bb49c4574e678ca 100644 (file)
@@ -129,8 +129,10 @@ PyObject *_PyCodec_Lookup(const char *encoding)
 
     /* Next, scan the search functions in order of registration */
     args = PyTuple_New(1);
-    if (args == NULL)
-        goto onError;
+    if (args == NULL) {
+        Py_DECREF(v);
+        return NULL;
+    }
     PyTuple_SET_ITEM(args,0,v);
 
     len = PyList_Size(interp->codec_search_path);
index 15637c6a1f31d14952d3deb6804f143ef9d8170a..c4f087761cd65fd2121b875580091c088c2c0857 100644 (file)
@@ -2143,10 +2143,10 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
     }
 
     mod = _PyImport_FindExtensionObject(name, path);
-    if (mod != NULL) {
+    if (mod != NULL || PyErr_Occurred()) {
         Py_DECREF(name);
         Py_DECREF(path);
-        Py_INCREF(mod);
+        Py_XINCREF(mod);
         return mod;
     }
 
index 6de32decc5aed37ec4c9f3078e7d6ea5aa0a99b2..37ecc510c8fba4086c3bdf1311999b25e8eec92b 100644 (file)
@@ -1286,6 +1286,9 @@ new_interpreter(PyThreadState **tstate_p)
         PyDict_SetItemString(interp->sysdict, "modules", modules);
         _PySys_EndInit(interp->sysdict, interp);
     }
+    else if (PyErr_Occurred()) {
+        goto handle_error;
+    }
 
     bimod = _PyImport_FindBuiltin("builtins", modules);
     if (bimod != NULL) {
@@ -1294,6 +1297,9 @@ new_interpreter(PyThreadState **tstate_p)
             goto handle_error;
         Py_INCREF(interp->builtins);
     }
+    else if (PyErr_Occurred()) {
+        goto handle_error;
+    }
 
     /* initialize builtin exceptions */
     _PyExc_Init(bimod);