mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +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
|
@ -3326,19 +3326,15 @@ PyObject *
|
|||
PyImport_GetImporter(PyObject *path)
|
||||
{
|
||||
PyThreadState *tstate = _PyThreadState_GET();
|
||||
PyObject *path_importer_cache = PySys_GetObject("path_importer_cache");
|
||||
PyObject *path_importer_cache = _PySys_GetRequiredAttrString("path_importer_cache");
|
||||
if (path_importer_cache == NULL) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "lost sys.path_importer_cache");
|
||||
return NULL;
|
||||
}
|
||||
Py_INCREF(path_importer_cache);
|
||||
PyObject *path_hooks = PySys_GetObject("path_hooks");
|
||||
PyObject *path_hooks = _PySys_GetRequiredAttrString("path_hooks");
|
||||
if (path_hooks == NULL) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "lost sys.path_hooks");
|
||||
Py_DECREF(path_importer_cache);
|
||||
return NULL;
|
||||
}
|
||||
Py_INCREF(path_hooks);
|
||||
PyObject *importer = get_path_importer(tstate, path_importer_cache, path_hooks, path);
|
||||
Py_DECREF(path_hooks);
|
||||
Py_DECREF(path_importer_cache);
|
||||
|
@ -3640,15 +3636,31 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name)
|
|||
|
||||
PyTime_t t1 = 0, accumulated_copy = accumulated;
|
||||
|
||||
PyObject *sys_path = PySys_GetObject("path");
|
||||
PyObject *sys_meta_path = PySys_GetObject("meta_path");
|
||||
PyObject *sys_path_hooks = PySys_GetObject("path_hooks");
|
||||
PyObject *sys_path, *sys_meta_path, *sys_path_hooks;
|
||||
if (_PySys_GetOptionalAttrString("path", &sys_path) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (_PySys_GetOptionalAttrString("meta_path", &sys_meta_path) < 0) {
|
||||
Py_XDECREF(sys_path);
|
||||
return NULL;
|
||||
}
|
||||
if (_PySys_GetOptionalAttrString("path_hooks", &sys_path_hooks) < 0) {
|
||||
Py_XDECREF(sys_meta_path);
|
||||
Py_XDECREF(sys_path);
|
||||
return NULL;
|
||||
}
|
||||
if (_PySys_Audit(tstate, "import", "OOOOO",
|
||||
abs_name, Py_None, sys_path ? sys_path : Py_None,
|
||||
sys_meta_path ? sys_meta_path : Py_None,
|
||||
sys_path_hooks ? sys_path_hooks : Py_None) < 0) {
|
||||
Py_XDECREF(sys_path_hooks);
|
||||
Py_XDECREF(sys_meta_path);
|
||||
Py_XDECREF(sys_path);
|
||||
return NULL;
|
||||
}
|
||||
Py_XDECREF(sys_path_hooks);
|
||||
Py_XDECREF(sys_meta_path);
|
||||
Py_XDECREF(sys_path);
|
||||
|
||||
|
||||
/* XOptions is initialized after first some imports.
|
||||
|
@ -4074,10 +4086,8 @@ _PyImport_FiniCore(PyInterpreterState *interp)
|
|||
static int
|
||||
init_zipimport(PyThreadState *tstate, int verbose)
|
||||
{
|
||||
PyObject *path_hooks = PySys_GetObject("path_hooks");
|
||||
PyObject *path_hooks = _PySys_GetRequiredAttrString("path_hooks");
|
||||
if (path_hooks == NULL) {
|
||||
_PyErr_SetString(tstate, PyExc_RuntimeError,
|
||||
"unable to get sys.path_hooks");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -4097,12 +4107,14 @@ init_zipimport(PyThreadState *tstate, int verbose)
|
|||
int err = PyList_Insert(path_hooks, 0, zipimporter);
|
||||
Py_DECREF(zipimporter);
|
||||
if (err < 0) {
|
||||
Py_DECREF(path_hooks);
|
||||
return -1;
|
||||
}
|
||||
if (verbose) {
|
||||
PySys_WriteStderr("# installed zipimport hook\n");
|
||||
}
|
||||
}
|
||||
Py_DECREF(path_hooks);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue