summaryrefslogtreecommitdiffstats
path: root/Python/errors.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-08-19 11:51:03 (GMT)
committerGitHub <noreply@github.com>2023-08-19 11:51:03 (GMT)
commit633ea217a85f6b6ba5bdbc73094254d5811b3485 (patch)
treec33fba2ad466d260ebdce2e743ca6df9e28a3152 /Python/errors.c
parent79db9d9a0e8f51ad4ea5caae31d7ae4b29985271 (diff)
downloadcpython-633ea217a85f6b6ba5bdbc73094254d5811b3485.zip
cpython-633ea217a85f6b6ba5bdbc73094254d5811b3485.tar.gz
cpython-633ea217a85f6b6ba5bdbc73094254d5811b3485.tar.bz2
gh-107915: Handle errors in C API functions PyErr_Set*() and PyErr_Format() (GH-107918)
Such C API functions as PyErr_SetString(), PyErr_Format(), PyErr_SetFromErrnoWithFilename() and many others no longer crash or ignore errors if it failed to format the error message or decode the filename. Instead, they keep a corresponding error.
Diffstat (limited to 'Python/errors.c')
-rw-r--r--Python/errors.c37
1 files changed, 29 insertions, 8 deletions
diff --git a/Python/errors.c b/Python/errors.c
index 916958c..d9086c5 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -292,8 +292,10 @@ _PyErr_SetString(PyThreadState *tstate, PyObject *exception,
const char *string)
{
PyObject *value = PyUnicode_FromString(string);
- _PyErr_SetObject(tstate, exception, value);
- Py_XDECREF(value);
+ if (value != NULL) {
+ _PyErr_SetObject(tstate, exception, value);
+ Py_DECREF(value);
+ }
}
void
@@ -891,7 +893,13 @@ PyErr_SetFromErrnoWithFilenameObjects(PyObject *exc, PyObject *filenameObject, P
PyObject *
PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
{
- PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
+ PyObject *name = NULL;
+ if (filename) {
+ name = PyUnicode_DecodeFSDefault(filename);
+ if (name == NULL) {
+ return NULL;
+ }
+ }
PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL);
Py_XDECREF(name);
return result;
@@ -988,7 +996,13 @@ PyObject *PyErr_SetExcFromWindowsErrWithFilename(
int ierr,
const char *filename)
{
- PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
+ PyObject *name = NULL;
+ if (filename) {
+ name = PyUnicode_DecodeFSDefault(filename);
+ if (name == NULL) {
+ return NULL;
+ }
+ }
PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObjects(exc,
ierr,
name,
@@ -1012,7 +1026,13 @@ PyObject *PyErr_SetFromWindowsErrWithFilename(
int ierr,
const char *filename)
{
- PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
+ PyObject *name = NULL;
+ if (filename) {
+ name = PyUnicode_DecodeFSDefault(filename);
+ if (name == NULL) {
+ return NULL;
+ }
+ }
PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObjects(
PyExc_OSError,
ierr, name, NULL);
@@ -1137,9 +1157,10 @@ _PyErr_FormatV(PyThreadState *tstate, PyObject *exception,
_PyErr_Clear(tstate);
string = PyUnicode_FromFormatV(format, vargs);
-
- _PyErr_SetObject(tstate, exception, string);
- Py_XDECREF(string);
+ if (string != NULL) {
+ _PyErr_SetObject(tstate, exception, string);
+ Py_DECREF(string);
+ }
return NULL;
}