mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
gh-130163: Fix crashes related to PySys_GetObject() (GH-130503)
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().
This commit is contained in:
parent
2dad1e08ec
commit
0ef4ffeefd
23 changed files with 513 additions and 213 deletions
|
@ -8,7 +8,7 @@
|
|||
#include "pycore_modsupport.h" // _PyArg_NoKeywords()
|
||||
#include "pycore_pylifecycle.h"
|
||||
#include "pycore_pystate.h" // _PyThreadState_SetCurrent()
|
||||
#include "pycore_sysmodule.h" // _PySys_GetAttr()
|
||||
#include "pycore_sysmodule.h" // _PySys_GetOptionalAttr()
|
||||
#include "pycore_time.h" // _PyTime_FromSeconds()
|
||||
#include "pycore_weakref.h" // _PyWeakref_GET_REF()
|
||||
|
||||
|
@ -2249,9 +2249,12 @@ thread_excepthook(PyObject *module, PyObject *args)
|
|||
PyObject *exc_tb = PyStructSequence_GET_ITEM(args, 2);
|
||||
PyObject *thread = PyStructSequence_GET_ITEM(args, 3);
|
||||
|
||||
PyThreadState *tstate = _PyThreadState_GET();
|
||||
PyObject *file = _PySys_GetAttr(tstate, &_Py_ID(stderr));
|
||||
PyObject *file;
|
||||
if (_PySys_GetOptionalAttr( &_Py_ID(stderr), &file) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (file == NULL || file == Py_None) {
|
||||
Py_XDECREF(file);
|
||||
if (thread == Py_None) {
|
||||
/* do nothing if sys.stderr is None and thread is None */
|
||||
Py_RETURN_NONE;
|
||||
|
@ -2268,9 +2271,6 @@ thread_excepthook(PyObject *module, PyObject *args)
|
|||
Py_RETURN_NONE;
|
||||
}
|
||||
}
|
||||
else {
|
||||
Py_INCREF(file);
|
||||
}
|
||||
|
||||
int res = thread_excepthook_file(file, exc_type, exc_value, exc_tb,
|
||||
thread);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue