Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e74680b

Browse files
gh-122595: Add more error checks in the compiler (GH-122596)
1 parent 94a4bd7 commit e74680b

File tree

2 files changed

+145
-52
lines changed

2 files changed

+145
-52
lines changed

Python/compile.c

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -505,21 +505,35 @@ dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
505505
deterministic, then the generated bytecode is not deterministic.
506506
*/
507507
sorted_keys = PyDict_Keys(src);
508-
if (sorted_keys == NULL)
508+
if (sorted_keys == NULL) {
509+
Py_DECREF(dest);
509510
return NULL;
511+
}
510512
if (PyList_Sort(sorted_keys) != 0) {
511513
Py_DECREF(sorted_keys);
514+
Py_DECREF(dest);
512515
return NULL;
513516
}
514517
num_keys = PyList_GET_SIZE(sorted_keys);
515518

516519
for (key_i = 0; key_i < num_keys; key_i++) {
517-
/* XXX this should probably be a macro in symtable.h */
518-
long vi;
519520
k = PyList_GET_ITEM(sorted_keys, key_i);
520521
v = PyDict_GetItemWithError(src, k);
521-
assert(v && PyLong_Check(v));
522-
vi = PyLong_AS_LONG(v);
522+
if (!v) {
523+
if (!PyErr_Occurred()) {
524+
PyErr_SetObject(PyExc_KeyError, k);
525+
}
526+
Py_DECREF(sorted_keys);
527+
Py_DECREF(dest);
528+
return NULL;
529+
}
530+
long vi = PyLong_AsLong(v);
531+
if (vi == -1 && PyErr_Occurred()) {
532+
Py_DECREF(sorted_keys);
533+
Py_DECREF(dest);
534+
return NULL;
535+
}
536+
/* XXX this should probably be a macro in symtable.h */
523537
scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
524538

525539
if (scope == scope_type || vi & flag) {
@@ -631,6 +645,7 @@ compiler_set_qualname(struct compiler *c)
631645

632646
scope = _PyST_GetScope(parent->u_ste, mangled);
633647
Py_DECREF(mangled);
648+
RETURN_IF_ERROR(scope);
634649
assert(scope != GLOBAL_IMPLICIT);
635650
if (scope == GLOBAL_EXPLICIT)
636651
force_global = 1;
@@ -1648,7 +1663,7 @@ dict_lookup_arg(PyObject *dict, PyObject *name)
16481663
if (v == NULL) {
16491664
return ERROR;
16501665
}
1651-
return PyLong_AS_LONG(v);
1666+
return PyLong_AsLong(v);
16521667
}
16531668

16541669
static int
@@ -1671,7 +1686,7 @@ compiler_lookup_arg(struct compiler *c, PyCodeObject *co, PyObject *name)
16711686
else {
16721687
arg = dict_lookup_arg(c->u->u_metadata.u_freevars, name);
16731688
}
1674-
if (arg == -1) {
1689+
if (arg == -1 && !PyErr_Occurred()) {
16751690
PyObject *freevars = _PyCode_GetFreevars(co);
16761691
if (freevars == NULL) {
16771692
PyErr_Clear();
@@ -4085,6 +4100,8 @@ compiler_nameop(struct compiler *c, location loc,
40854100
case GLOBAL_EXPLICIT:
40864101
optype = OP_GLOBAL;
40874102
break;
4103+
case -1:
4104+
goto error;
40884105
default:
40894106
/* scope can be 0 */
40904107
break;
@@ -4638,6 +4655,7 @@ is_import_originated(struct compiler *c, expr_ty e)
46384655
}
46394656

46404657
long flags = _PyST_GetSymbol(SYMTABLE(c)->st_top, e->v.Name.id);
4658+
RETURN_IF_ERROR(flags);
46414659
return flags & DEF_IMPORT;
46424660
}
46434661

@@ -4657,10 +4675,12 @@ can_optimize_super_call(struct compiler *c, expr_ty attr)
46574675
PyObject *super_name = e->v.Call.func->v.Name.id;
46584676
// detect statically-visible shadowing of 'super' name
46594677
int scope = _PyST_GetScope(SYMTABLE_ENTRY(c), super_name);
4678+
RETURN_IF_ERROR(scope);
46604679
if (scope != GLOBAL_IMPLICIT) {
46614680
return 0;
46624681
}
46634682
scope = _PyST_GetScope(SYMTABLE(c)->st_top, super_name);
4683+
RETURN_IF_ERROR(scope);
46644684
if (scope != 0) {
46654685
return 0;
46664686
}
@@ -4767,7 +4787,9 @@ maybe_optimize_method_call(struct compiler *c, expr_ty e)
47674787
}
47684788

47694789
/* Check that the base object is not something that is imported */
4770-
if (is_import_originated(c, meth->v.Attribute.value)) {
4790+
int ret = is_import_originated(c, meth->v.Attribute.value);
4791+
RETURN_IF_ERROR(ret);
4792+
if (ret) {
47714793
return 0;
47724794
}
47734795

@@ -4795,7 +4817,9 @@ maybe_optimize_method_call(struct compiler *c, expr_ty e)
47954817
/* Alright, we can optimize the code. */
47964818
location loc = LOC(meth);
47974819

4798-
if (can_optimize_super_call(c, meth)) {
4820+
ret = can_optimize_super_call(c, meth);
4821+
RETURN_IF_ERROR(ret);
4822+
if (ret) {
47994823
RETURN_IF_ERROR(load_args_for_super(c, meth->v.Attribute.value));
48004824
int opcode = asdl_seq_LEN(meth->v.Attribute.value->v.Call.args) ?
48014825
LOAD_SUPER_METHOD : LOAD_ZERO_SUPER_METHOD;
@@ -5367,8 +5391,10 @@ push_inlined_comprehension_state(struct compiler *c, location loc,
53675391
PyObject *k, *v;
53685392
Py_ssize_t pos = 0;
53695393
while (PyDict_Next(entry->ste_symbols, &pos, &k, &v)) {
5370-
assert(PyLong_Check(v));
5371-
long symbol = PyLong_AS_LONG(v);
5394+
long symbol = PyLong_AsLong(v);
5395+
if (symbol == -1 && PyErr_Occurred()) {
5396+
return ERROR;
5397+
}
53725398
long scope = (symbol >> SCOPE_OFFSET) & SCOPE_MASK;
53735399
PyObject *outv = PyDict_GetItemWithError(SYMTABLE_ENTRY(c)->ste_symbols, k);
53745400
if (outv == NULL) {
@@ -5377,8 +5403,11 @@ push_inlined_comprehension_state(struct compiler *c, location loc,
53775403
}
53785404
outv = _PyLong_GetZero();
53795405
}
5380-
assert(PyLong_CheckExact(outv));
5381-
long outsc = (PyLong_AS_LONG(outv) >> SCOPE_OFFSET) & SCOPE_MASK;
5406+
long outsymbol = PyLong_AsLong(outv);
5407+
if (outsymbol == -1 && PyErr_Occurred()) {
5408+
return ERROR;
5409+
}
5410+
long outsc = (outsymbol >> SCOPE_OFFSET) & SCOPE_MASK;
53825411
// If a name has different scope inside than outside the comprehension,
53835412
// we need to temporarily handle it with the right scope while
53845413
// compiling the comprehension. If it's free in the comprehension
@@ -6064,14 +6093,18 @@ compiler_visit_expr(struct compiler *c, expr_ty e)
60646093
return compiler_formatted_value(c, e);
60656094
/* The following exprs can be assignment targets. */
60666095
case Attribute_kind:
6067-
if (e->v.Attribute.ctx == Load && can_optimize_super_call(c, e)) {
6068-
RETURN_IF_ERROR(load_args_for_super(c, e->v.Attribute.value));
6069-
int opcode = asdl_seq_LEN(e->v.Attribute.value->v.Call.args) ?
6070-
LOAD_SUPER_ATTR : LOAD_ZERO_SUPER_ATTR;
6071-
ADDOP_NAME(c, loc, opcode, e->v.Attribute.attr, names);
6072-
loc = update_start_location_to_match_attr(c, loc, e);
6073-
ADDOP(c, loc, NOP);
6074-
return SUCCESS;
6096+
if (e->v.Attribute.ctx == Load) {
6097+
int ret = can_optimize_super_call(c, e);
6098+
RETURN_IF_ERROR(ret);
6099+
if (ret) {
6100+
RETURN_IF_ERROR(load_args_for_super(c, e->v.Attribute.value));
6101+
int opcode = asdl_seq_LEN(e->v.Attribute.value->v.Call.args) ?
6102+
LOAD_SUPER_ATTR : LOAD_ZERO_SUPER_ATTR;
6103+
ADDOP_NAME(c, loc, opcode, e->v.Attribute.attr, names);
6104+
loc = update_start_location_to_match_attr(c, loc, e);
6105+
ADDOP(c, loc, NOP);
6106+
return SUCCESS;
6107+
}
60756108
}
60766109
RETURN_IF_ERROR(compiler_maybe_add_static_attribute_to_class(c, e));
60776110
VISIT(c, expr, e->v.Attribute.value);
@@ -7300,7 +7333,8 @@ consts_dict_keys_inorder(PyObject *dict)
73007333
if (consts == NULL)
73017334
return NULL;
73027335
while (PyDict_Next(dict, &pos, &k, &v)) {
7303-
i = PyLong_AS_LONG(v);
7336+
assert(PyLong_CheckExact(v));
7337+
i = PyLong_AsLong(v);
73047338
/* The keys of the dictionary can be tuples wrapping a constant.
73057339
* (see dict_add_o and _PyCode_ConstantKey). In that case
73067340
* the object we want is always second. */

0 commit comments

Comments
 (0)