Skip to content

Commit 6d42295

Browse files
erlend-aaslandpull[bot]
authored andcommitted
gh-105375: Improve PyErr_WarnExplicit() error handling (#105610)
Bail on first error to prevent exceptions from possibly being overwritten.
1 parent 827711c commit 6d42295

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a bug in :c:func:`PyErr_WarnExplicit` where an exception could end up
2+
being overwritten if the API failed internally.

Python/_warnings.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,25 +1298,29 @@ PyErr_WarnExplicit(PyObject *category, const char *text,
12981298
const char *module_str, PyObject *registry)
12991299
{
13001300
PyObject *message = PyUnicode_FromString(text);
1301+
if (message == NULL) {
1302+
return -1;
1303+
}
13011304
PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
1305+
if (filename == NULL) {
1306+
Py_DECREF(message);
1307+
return -1;
1308+
}
13021309
PyObject *module = NULL;
1303-
int ret = -1;
1304-
1305-
if (message == NULL || filename == NULL)
1306-
goto exit;
13071310
if (module_str != NULL) {
13081311
module = PyUnicode_FromString(module_str);
1309-
if (module == NULL)
1310-
goto exit;
1312+
if (module == NULL) {
1313+
Py_DECREF(filename);
1314+
Py_DECREF(message);
1315+
return -1;
1316+
}
13111317
}
13121318

1313-
ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1314-
module, registry);
1315-
1316-
exit:
1317-
Py_XDECREF(message);
1319+
int ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1320+
module, registry);
13181321
Py_XDECREF(module);
1319-
Py_XDECREF(filename);
1322+
Py_DECREF(filename);
1323+
Py_DECREF(message);
13201324
return ret;
13211325
}
13221326

0 commit comments

Comments
 (0)