mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
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.
This commit is contained in:
parent
7c97dc8c95
commit
7d2ffada0a
2 changed files with 36 additions and 21 deletions
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue