mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
[3.13] gh-130163: Fix crashes related to PySys_GetObject() (GH-130503) (GH-130556)
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 0ef4ffeefd
)
This commit is contained in:
parent
b0d3f49195
commit
7c1b76fce8
23 changed files with 505 additions and 180 deletions
|
@ -3341,19 +3341,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);
|
||||
|
@ -3655,15 +3651,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.
|
||||
|
@ -4111,10 +4123,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;
|
||||
}
|
||||
|
||||
|
@ -4134,12 +4144,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