mirror of
https://github.com/python/cpython.git
synced 2026-01-04 06:22:20 +00:00
[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 commit0ef4ffeefd) (cherry picked from commit7c1b76fce8) (cherry picked from commit2ab7e1135a)
This commit is contained in:
parent
6a268a046f
commit
89a79fc919
21 changed files with 506 additions and 162 deletions
|
|
@ -9,6 +9,7 @@
|
|||
#include "pycore_pylifecycle.h" // _Py_PreInitializeFromConfig()
|
||||
#include "pycore_pymem.h" // _PyMem_SetDefaultAllocator()
|
||||
#include "pycore_pystate.h" // _PyThreadState_GET()
|
||||
#include "pycore_sysmodule.h" // _PySys_GetOptionalAttrString()
|
||||
|
||||
#include "osdefs.h" // DELIM
|
||||
|
||||
|
|
@ -3144,10 +3145,13 @@ _Py_DumpPathConfig(PyThreadState *tstate)
|
|||
|
||||
#define DUMP_SYS(NAME) \
|
||||
do { \
|
||||
obj = PySys_GetObject(#NAME); \
|
||||
PySys_FormatStderr(" sys.%s = ", #NAME); \
|
||||
if (_PySys_GetOptionalAttrString(#NAME, &obj) < 0) { \
|
||||
PyErr_Clear(); \
|
||||
} \
|
||||
if (obj != NULL) { \
|
||||
PySys_FormatStderr("%A", obj); \
|
||||
Py_DECREF(obj); \
|
||||
} \
|
||||
else { \
|
||||
PySys_WriteStderr("(not set)"); \
|
||||
|
|
@ -3165,7 +3169,8 @@ _Py_DumpPathConfig(PyThreadState *tstate)
|
|||
DUMP_SYS(exec_prefix);
|
||||
#undef DUMP_SYS
|
||||
|
||||
PyObject *sys_path = PySys_GetObject("path"); /* borrowed reference */
|
||||
PyObject *sys_path;
|
||||
(void) _PySys_GetOptionalAttrString("path", &sys_path);
|
||||
if (sys_path != NULL && PyList_Check(sys_path)) {
|
||||
PySys_WriteStderr(" sys.path = [\n");
|
||||
Py_ssize_t len = PyList_GET_SIZE(sys_path);
|
||||
|
|
@ -3175,6 +3180,7 @@ _Py_DumpPathConfig(PyThreadState *tstate)
|
|||
}
|
||||
PySys_WriteStderr(" ]\n");
|
||||
}
|
||||
Py_XDECREF(sys_path);
|
||||
|
||||
_PyErr_SetRaisedException(tstate, exc);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue