summaryrefslogtreecommitdiffstats
path: root/Python/pythonrun.c
diff options
context:
space:
mode:
authorNGRsoftlab <78017794+NGRsoftlab@users.noreply.github.com>2024-05-02 13:43:03 (GMT)
committerGitHub <noreply@github.com>2024-05-02 13:43:03 (GMT)
commit7d2ffada0a6490e6839697f729bcd80380e9f561 (patch)
tree2406de9015978d0b64b082e17d711677c1743b0d /Python/pythonrun.c
parent7c97dc8c9594c71bd3d1f69758a27de45f57e4c3 (diff)
downloadcpython-7d2ffada0a6490e6839697f729bcd80380e9f561.zip
cpython-7d2ffada0a6490e6839697f729bcd80380e9f561.tar.gz
cpython-7d2ffada0a6490e6839697f729bcd80380e9f561.tar.bz2
gh-116180: Check the globals argument in PyRun_* C API (GH-116637)
It used to crash when passing NULL or non-dict as globals. Now it sets a SystemError.
Diffstat (limited to 'Python/pythonrun.c')
-rw-r--r--Python/pythonrun.c21
1 files changed, 12 insertions, 9 deletions
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 2970248..31213ae 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -1275,17 +1275,20 @@ run_eval_code_obj(PyThreadState *tstate, PyCodeObject *co, PyObject *globals, Py
_PyRuntime.signals.unhandled_keyboard_interrupt = 0;
/* Set globals['__builtins__'] if it doesn't exist */
- if (globals != NULL) {
- int has_builtins = PyDict_ContainsString(globals, "__builtins__");
- if (has_builtins < 0) {
+ if (!globals || !PyDict_Check(globals)) {
+ PyErr_SetString(PyExc_SystemError, "globals must be a real dict");
+ return NULL;
+ }
+ int has_builtins = PyDict_ContainsString(globals, "__builtins__");
+ if (has_builtins < 0) {
+ return NULL;
+ }
+ if (!has_builtins) {
+ if (PyDict_SetItemString(globals, "__builtins__",
+ tstate->interp->builtins) < 0)
+ {
return NULL;
}
- if (!has_builtins) {
- if (PyDict_SetItemString(globals, "__builtins__",
- tstate->interp->builtins) < 0) {
- return NULL;
- }
- }
}
v = PyEval_EvalCode((PyObject*)co, globals, locals);