Skip to content

Commit 62674f3

Browse files
bpo-35454: Fix miscellaneous minor issues in error handling. (GH-11077)
* bpo-35454: Fix miscellaneous minor issues in error handling. * Fix a null pointer dereference. (cherry picked from commit 8905fcc) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 3b9a018 commit 62674f3

File tree

9 files changed

+44
-23
lines changed

9 files changed

+44
-23
lines changed

Modules/_elementtree.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,10 @@ get_attrib_from_keywords(PyObject *kwds)
352352
return NULL;
353353
}
354354
attrib = PyDict_Copy(attrib);
355-
PyDict_DelItem(kwds, attrib_str);
355+
if (attrib && PyDict_DelItem(kwds, attrib_str) < 0) {
356+
Py_DECREF(attrib);
357+
attrib = NULL;
358+
}
356359
} else {
357360
attrib = PyDict_New();
358361
}

Modules/_threadmodule.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -777,9 +777,11 @@ local_clear(localobject *self)
777777
for(tstate = PyInterpreterState_ThreadHead(tstate->interp);
778778
tstate;
779779
tstate = PyThreadState_Next(tstate))
780-
if (tstate->dict &&
781-
PyDict_GetItem(tstate->dict, self->key))
782-
PyDict_DelItem(tstate->dict, self->key);
780+
if (tstate->dict && PyDict_GetItem(tstate->dict, self->key)) {
781+
if (PyDict_DelItem(tstate->dict, self->key)) {
782+
PyErr_Clear();
783+
}
784+
}
783785
}
784786
return 0;
785787
}

Modules/socketmodule.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,14 @@ remove_unusable_flags(PyObject *m)
360360
else {
361361
if (PyDict_GetItemString(
362362
dict,
363-
win_runtime_flags[i].flag_name) != NULL) {
364-
PyDict_DelItemString(
365-
dict,
366-
win_runtime_flags[i].flag_name);
363+
win_runtime_flags[i].flag_name) != NULL)
364+
{
365+
if (PyDict_DelItemString(
366+
dict,
367+
win_runtime_flags[i].flag_name))
368+
{
369+
PyErr_Clear();
370+
}
367371
}
368372
}
369373
}

Objects/namespaceobject.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ namespace_repr(PyObject *ns)
103103
PyObject *value, *item;
104104

105105
value = PyDict_GetItem(d, key);
106-
assert(value != NULL);
107-
108-
item = PyUnicode_FromFormat("%S=%R", key, value);
109-
if (item == NULL) {
110-
loop_error = 1;
111-
}
112-
else {
113-
loop_error = PyList_Append(pairs, item);
114-
Py_DECREF(item);
106+
if (value != NULL) {
107+
item = PyUnicode_FromFormat("%S=%R", key, value);
108+
if (item == NULL) {
109+
loop_error = 1;
110+
}
111+
else {
112+
loop_error = PyList_Append(pairs, item);
113+
Py_DECREF(item);
114+
}
115115
}
116116
}
117117

Python/_warnings.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,11 @@ already_warned(PyObject *registry, PyObject *key, int should_set)
256256
version_obj = _PyDict_GetItemId(registry, &PyId_version);
257257
if (version_obj == NULL
258258
|| !PyLong_CheckExact(version_obj)
259-
|| PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version) {
259+
|| PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version)
260+
{
261+
if (PyErr_Occurred()) {
262+
return -1;
263+
}
260264
PyDict_Clear(registry);
261265
version_obj = PyLong_FromLong(_PyRuntime.warnings.filters_version);
262266
if (version_obj == NULL)

Python/ceval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5027,7 +5027,7 @@ unicode_concatenate(PyObject *v, PyObject *w,
50275027
PyObject *names = f->f_code->co_names;
50285028
PyObject *name = GETITEM(names, oparg);
50295029
PyObject *locals = f->f_locals;
5030-
if (PyDict_CheckExact(locals) &&
5030+
if (locals && PyDict_CheckExact(locals) &&
50315031
PyDict_GetItem(locals, name) == v) {
50325032
if (PyDict_DelItem(locals, name) != 0) {
50335033
PyErr_Clear();

Python/codecs.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,10 @@ PyObject *_PyCodec_Lookup(const char *encoding)
130130

131131
/* Next, scan the search functions in order of registration */
132132
args = PyTuple_New(1);
133-
if (args == NULL)
134-
goto onError;
133+
if (args == NULL) {
134+
Py_DECREF(v);
135+
return NULL;
136+
}
135137
PyTuple_SET_ITEM(args,0,v);
136138

137139
len = PyList_Size(interp->codec_search_path);

Python/import.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,10 +2138,10 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
21382138
}
21392139

21402140
mod = _PyImport_FindExtensionObject(name, path);
2141-
if (mod != NULL) {
2141+
if (mod != NULL || PyErr_Occurred()) {
21422142
Py_DECREF(name);
21432143
Py_DECREF(path);
2144-
Py_INCREF(mod);
2144+
Py_XINCREF(mod);
21452145
return mod;
21462146
}
21472147

Python/pylifecycle.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,9 @@ new_interpreter(PyThreadState **tstate_p)
14171417
PyDict_SetItemString(interp->sysdict, "modules", modules);
14181418
_PySys_EndInit(interp->sysdict, &interp->config);
14191419
}
1420+
else if (PyErr_Occurred()) {
1421+
goto handle_error;
1422+
}
14201423

14211424
bimod = _PyImport_FindBuiltin("builtins", modules);
14221425
if (bimod != NULL) {
@@ -1425,6 +1428,9 @@ new_interpreter(PyThreadState **tstate_p)
14251428
goto handle_error;
14261429
Py_INCREF(interp->builtins);
14271430
}
1431+
else if (PyErr_Occurred()) {
1432+
goto handle_error;
1433+
}
14281434

14291435
/* initialize builtin exceptions */
14301436
_PyExc_Init(bimod);

0 commit comments

Comments
 (0)