Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-122595: Add more error checks in the compiler #122596

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 56 additions & 22 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -505,21 +505,35 @@ dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
deterministic, then the generated bytecode is not deterministic.
*/
sorted_keys = PyDict_Keys(src);
if (sorted_keys == NULL)
if (sorted_keys == NULL) {
Py_DECREF(dest);
return NULL;
}
if (PyList_Sort(sorted_keys) != 0) {
Py_DECREF(sorted_keys);
Py_DECREF(dest);
return NULL;
}
num_keys = PyList_GET_SIZE(sorted_keys);

for (key_i = 0; key_i < num_keys; key_i++) {
/* XXX this should probably be a macro in symtable.h */
long vi;
k = PyList_GET_ITEM(sorted_keys, key_i);
v = PyDict_GetItemWithError(src, k);
assert(v && PyLong_Check(v));
vi = PyLong_AS_LONG(v);
if (!v) {
if (!PyErr_Occurred()) {
PyErr_SetObject(PyExc_KeyError, k);
}
Py_DECREF(sorted_keys);
Py_DECREF(dest);
return NULL;
}
long vi = PyLong_AsLong(v);
if (vi == -1 && PyErr_Occurred()) {
Py_DECREF(sorted_keys);
Py_DECREF(dest);
return NULL;
}
/* XXX this should probably be a macro in symtable.h */
scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;

if (scope == scope_type || vi & flag) {
Expand Down Expand Up @@ -623,6 +637,7 @@ compiler_set_qualname(struct compiler *c)

scope = _PyST_GetScope(parent->u_ste, mangled);
Py_DECREF(mangled);
RETURN_IF_ERROR(scope);
assert(scope != GLOBAL_IMPLICIT);
if (scope == GLOBAL_EXPLICIT)
force_global = 1;
Expand Down Expand Up @@ -1640,7 +1655,7 @@ dict_lookup_arg(PyObject *dict, PyObject *name)
if (v == NULL) {
return ERROR;
}
return PyLong_AS_LONG(v);
return PyLong_AsLong(v);
}

static int
Expand All @@ -1663,7 +1678,7 @@ compiler_lookup_arg(struct compiler *c, PyCodeObject *co, PyObject *name)
else {
arg = dict_lookup_arg(c->u->u_metadata.u_freevars, name);
}
if (arg == -1) {
if (arg == -1 && !PyErr_Occurred()) {
PyObject *freevars = _PyCode_GetFreevars(co);
if (freevars == NULL) {
PyErr_Clear();
Expand Down Expand Up @@ -4077,6 +4092,8 @@ compiler_nameop(struct compiler *c, location loc,
case GLOBAL_EXPLICIT:
optype = OP_GLOBAL;
break;
case -1:
goto error;
default:
/* scope can be 0 */
break;
Expand Down Expand Up @@ -4630,6 +4647,7 @@ is_import_originated(struct compiler *c, expr_ty e)
}

long flags = _PyST_GetSymbol(SYMTABLE(c)->st_top, e->v.Name.id);
RETURN_IF_ERROR(flags);
return flags & DEF_IMPORT;
}

Expand All @@ -4649,10 +4667,12 @@ can_optimize_super_call(struct compiler *c, expr_ty attr)
PyObject *super_name = e->v.Call.func->v.Name.id;
// detect statically-visible shadowing of 'super' name
int scope = _PyST_GetScope(SYMTABLE_ENTRY(c), super_name);
RETURN_IF_ERROR(scope);
if (scope != GLOBAL_IMPLICIT) {
return 0;
}
scope = _PyST_GetScope(SYMTABLE(c)->st_top, super_name);
RETURN_IF_ERROR(scope);
if (scope != 0) {
return 0;
}
Expand Down Expand Up @@ -4759,7 +4779,9 @@ maybe_optimize_method_call(struct compiler *c, expr_ty e)
}

/* Check that the base object is not something that is imported */
if (is_import_originated(c, meth->v.Attribute.value)) {
int ret = is_import_originated(c, meth->v.Attribute.value);
RETURN_IF_ERROR(ret);
if (ret) {
return 0;
}

Expand Down Expand Up @@ -4787,7 +4809,9 @@ maybe_optimize_method_call(struct compiler *c, expr_ty e)
/* Alright, we can optimize the code. */
location loc = LOC(meth);

if (can_optimize_super_call(c, meth)) {
ret = can_optimize_super_call(c, meth);
RETURN_IF_ERROR(ret);
if (ret) {
RETURN_IF_ERROR(load_args_for_super(c, meth->v.Attribute.value));
int opcode = asdl_seq_LEN(meth->v.Attribute.value->v.Call.args) ?
LOAD_SUPER_METHOD : LOAD_ZERO_SUPER_METHOD;
Expand Down Expand Up @@ -5359,8 +5383,10 @@ push_inlined_comprehension_state(struct compiler *c, location loc,
PyObject *k, *v;
Py_ssize_t pos = 0;
while (PyDict_Next(entry->ste_symbols, &pos, &k, &v)) {
assert(PyLong_Check(v));
long symbol = PyLong_AS_LONG(v);
long symbol = PyLong_AsLong(v);
if (symbol == -1 && PyErr_Occurred()) {
return ERROR;
}
long scope = (symbol >> SCOPE_OFFSET) & SCOPE_MASK;
PyObject *outv = PyDict_GetItemWithError(SYMTABLE_ENTRY(c)->ste_symbols, k);
if (outv == NULL) {
Expand All @@ -5369,8 +5395,11 @@ push_inlined_comprehension_state(struct compiler *c, location loc,
}
outv = _PyLong_GetZero();
}
assert(PyLong_CheckExact(outv));
long outsc = (PyLong_AS_LONG(outv) >> SCOPE_OFFSET) & SCOPE_MASK;
long outsymbol = PyLong_AsLong(outv);
if (outsymbol == -1 && PyErr_Occurred()) {
return ERROR;
}
long outsc = (outsymbol >> SCOPE_OFFSET) & SCOPE_MASK;
// If a name has different scope inside than outside the comprehension,
// we need to temporarily handle it with the right scope while
// compiling the comprehension. If it's free in the comprehension
Expand Down Expand Up @@ -6056,14 +6085,18 @@ compiler_visit_expr(struct compiler *c, expr_ty e)
return compiler_formatted_value(c, e);
/* The following exprs can be assignment targets. */
case Attribute_kind:
if (e->v.Attribute.ctx == Load && can_optimize_super_call(c, e)) {
RETURN_IF_ERROR(load_args_for_super(c, e->v.Attribute.value));
int opcode = asdl_seq_LEN(e->v.Attribute.value->v.Call.args) ?
LOAD_SUPER_ATTR : LOAD_ZERO_SUPER_ATTR;
ADDOP_NAME(c, loc, opcode, e->v.Attribute.attr, names);
loc = update_start_location_to_match_attr(c, loc, e);
ADDOP(c, loc, NOP);
return SUCCESS;
if (e->v.Attribute.ctx == Load) {
int ret = can_optimize_super_call(c, e);
RETURN_IF_ERROR(ret);
if (ret) {
RETURN_IF_ERROR(load_args_for_super(c, e->v.Attribute.value));
int opcode = asdl_seq_LEN(e->v.Attribute.value->v.Call.args) ?
LOAD_SUPER_ATTR : LOAD_ZERO_SUPER_ATTR;
ADDOP_NAME(c, loc, opcode, e->v.Attribute.attr, names);
loc = update_start_location_to_match_attr(c, loc, e);
ADDOP(c, loc, NOP);
return SUCCESS;
}
}
if (e->v.Attribute.value->kind == Name_kind &&
_PyUnicode_EqualToASCIIString(e->v.Attribute.value->v.Name.id, "self"))
Expand Down Expand Up @@ -7296,7 +7329,8 @@ consts_dict_keys_inorder(PyObject *dict)
if (consts == NULL)
return NULL;
while (PyDict_Next(dict, &pos, &k, &v)) {
i = PyLong_AS_LONG(v);
assert(PyLong_CheckExact(v));
i = PyLong_AsLong(v);
/* The keys of the dictionary can be tuples wrapping a constant.
* (see dict_add_o and _PyCode_ConstantKey). In that case
* the object we want is always second. */
Expand Down
Loading
Loading