aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/import.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2025-02-25 23:04:27 +0200
committerGitHub <noreply@github.com>2025-02-25 23:04:27 +0200
commit0ef4ffeefd1737c18dc9326133c7894d58108c2e (patch)
treedc3614077d06e68df192e57505951b80012a2e31 /Python/import.c
parent2dad1e08ec9d5ddc798a313900613b3d1eeaff6b (diff)
downloadcpython-0ef4ffeefd1737c18dc9326133c7894d58108c2e.tar.gz
cpython-0ef4ffeefd1737c18dc9326133c7894d58108c2e.zip
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().
Diffstat (limited to 'Python/import.c')
-rw-r--r--Python/import.c36
1 files changed, 24 insertions, 12 deletions
diff --git a/Python/import.c b/Python/import.c
index 6d3b42941bd..6836e13972c 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -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;
}