]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-35454: Fix miscellaneous minor issues in error handling. (GH-11077)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 11 Dec 2018 07:05:13 +0000 (23:05 -0800)
committerGitHub <noreply@github.com>
Tue, 11 Dec 2018 07:05:13 +0000 (23:05 -0800)
* bpo-35454: Fix miscellaneous minor issues in error handling.

* Fix a null pointer dereference.
(cherry picked from commit 8905fcc85a6fc3ac394bc89b0bbf40897e9497a6)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
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 3118b55c874ba549cb20aa34a76c128d834b7a27..ff3a240f2ec311faaec9e50758410ac3ccea3b96 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 16d3191c621a060d4d19d1e6f8733eb83a802780..2a6b8931d70cc8fae889e8a978aee4200016ed31 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 53bf996876265a8aa026aaff5d86913acd226eff..e6d3f8be6edeeaa67fbe142e41dc86dab9b663a5 100644 (file)
@@ -360,10 +360,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 7de102eb06a0162408bc7189d6a9b7d1d5d6d99c..c2f9d30d2998cd3245bc969ac9f0d73431a9a698 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 29e475d67d1f61d59b69d6fe171fc26c3ba40aab..80dca65fd353f84e2df2fe6ee4222fe028a19334 100644 (file)
@@ -256,7 +256,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 e371d345cc7498f55be2a8f8a9897c8d94518b85..00b2e6d85f4830b46b29a8b0ec1e26cfd2967f93 100644 (file)
@@ -5027,7 +5027,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 eb3cd35fb8e249a2506e221bebdb2d7d68be8233..c315ae3ed7eef507812161dac7c1233f87a5d6c5 100644 (file)
@@ -130,8 +130,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 709fc4324e6ffbe713de51c85190b6e7b17d069d..5f5d135c7a2ed29c621796b9add5ffff82d1ac3c 100644 (file)
@@ -2138,10 +2138,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 4b08c9c2b2ce85797a4328527e01e035d4b665d5..94b6d43c0e51cb72e67dea1e923ac905365f24d7 100644 (file)
@@ -1417,6 +1417,9 @@ new_interpreter(PyThreadState **tstate_p)
         PyDict_SetItemString(interp->sysdict, "modules", modules);
         _PySys_EndInit(interp->sysdict, &interp->config);
     }
+    else if (PyErr_Occurred()) {
+        goto handle_error;
+    }
 
     bimod = _PyImport_FindBuiltin("builtins", modules);
     if (bimod != NULL) {
@@ -1425,6 +1428,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);