diff options
| author | Serhiy Storchaka <storchaka@gmail.com> | 2025-02-26 15:20:47 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-26 15:20:47 (GMT) |
| commit | 89a79fc919419bfe817da13bc2a4437908d7fc07 (patch) | |
| tree | d8ca3bac7229ea443ff9d591b0459caac0b67cb1 /Python/errors.c | |
| parent | 6a268a046f474e860c8d0186a859fdd075d80463 (diff) | |
| download | cpython-89a79fc919419bfe817da13bc2a4437908d7fc07.zip cpython-89a79fc919419bfe817da13bc2a4437908d7fc07.tar.gz cpython-89a79fc919419bfe817da13bc2a4437908d7fc07.tar.bz2 | |
[3.12] gh-130163: Fix crashes related to PySys_GetObject() (GH-130503) (GH-130556) (GH-130576)
The use of PySys_GetObject() and _PySys_GetAttr(), which return a borrowed
reference, has been replaced by using one of the following functions, which
return a strong reference and distinguish a missing attribute from an error:
_PySys_GetOptionalAttr(), _PySys_GetOptionalAttrString(),
_PySys_GetRequiredAttr(), and _PySys_GetRequiredAttrString().
(cherry picked from commit 0ef4ffeefd1737c18dc9326133c7894d58108c2e)
(cherry picked from commit 7c1b76fce8c8df00da38830f72dbdde6881a33be)
(cherry picked from commit 2ab7e1135a2d5ca45b60881ece27729e4fc0ee8b)
Diffstat (limited to 'Python/errors.c')
| -rw-r--r-- | Python/errors.c | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/Python/errors.c b/Python/errors.c index bfc37f3..79cf4e0 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -1562,14 +1562,15 @@ write_unraisable_exc(PyThreadState *tstate, PyObject *exc_type, PyObject *exc_value, PyObject *exc_tb, PyObject *err_msg, PyObject *obj) { - PyObject *file = _PySys_GetAttr(tstate, &_Py_ID(stderr)); + PyObject *file; + if (_PySys_GetOptionalAttr(&_Py_ID(stderr), &file) < 0) { + return -1; + } if (file == NULL || file == Py_None) { + Py_XDECREF(file); return 0; } - /* Hold a strong reference to ensure that sys.stderr doesn't go away - while we use it */ - Py_INCREF(file); int res = write_unraisable_exc_file(tstate, exc_type, exc_value, exc_tb, err_msg, obj, file); Py_DECREF(file); @@ -1666,13 +1667,20 @@ _PyErr_WriteUnraisableMsg(const char *err_msg_str, PyObject *obj) goto error; } - PyObject *hook = _PySys_GetAttr(tstate, &_Py_ID(unraisablehook)); + PyObject *hook; + if (_PySys_GetOptionalAttr(&_Py_ID(unraisablehook), &hook) < 0) { + Py_DECREF(hook_args); + err_msg_str = NULL; + obj = NULL; + goto error; + } if (hook == NULL) { Py_DECREF(hook_args); goto default_hook; } if (_PySys_Audit(tstate, "sys.unraisablehook", "OO", hook, hook_args) < 0) { + Py_DECREF(hook); Py_DECREF(hook_args); err_msg_str = "Exception ignored in audit hook"; obj = NULL; @@ -1680,11 +1688,13 @@ _PyErr_WriteUnraisableMsg(const char *err_msg_str, PyObject *obj) } if (hook == Py_None) { + Py_DECREF(hook); Py_DECREF(hook_args); goto default_hook; } PyObject *res = PyObject_CallOneArg(hook, hook_args); + Py_DECREF(hook); Py_DECREF(hook_args); if (res != NULL) { Py_DECREF(res); |
