[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 0ef4ffeefd)
(cherry picked from commit 7c1b76fce8)
(cherry picked from commit 2ab7e1135a)
This commit is contained in:
Serhiy Storchaka 2025-02-26 17:20:47 +02:00 committed by GitHub
parent 6a268a046f
commit 89a79fc919
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 506 additions and 162 deletions

View file

@ -2431,19 +2431,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);
@ -2745,15 +2741,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.
@ -3207,10 +3219,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;
}
@ -3230,12 +3240,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;
}