mirror of
https://github.com/python/cpython.git
synced 2026-05-08 11:31:13 +00:00
[3.14] gh-148973: fix segfault on mismatch between consts size and oparg in compiler (GH-148974) (#148980)
gh-148973: fix segfault on mismatch between consts size and oparg in compiler (GH-148974)
(cherry picked from commit c650b51c32)
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
This commit is contained in:
parent
89f44ac422
commit
8acb98a1c2
5 changed files with 95 additions and 6 deletions
|
|
@ -1295,6 +1295,14 @@ get_const_value(int opcode, int oparg, PyObject *co_consts)
|
|||
PyObject *constant = NULL;
|
||||
assert(loads_const(opcode));
|
||||
if (opcode == LOAD_CONST) {
|
||||
assert(PyList_Check(co_consts));
|
||||
Py_ssize_t n = PyList_GET_SIZE(co_consts);
|
||||
if (oparg < 0 || oparg >= n) {
|
||||
PyErr_Format(PyExc_ValueError,
|
||||
"LOAD_CONST index %d is out of range for consts (len=%zd)",
|
||||
oparg, n);
|
||||
return NULL;
|
||||
}
|
||||
constant = PyList_GET_ITEM(co_consts, oparg);
|
||||
}
|
||||
if (opcode == LOAD_SMALL_INT) {
|
||||
|
|
@ -2153,6 +2161,9 @@ basicblock_optimize_load_const(PyObject *const_cache, basicblock *bb, PyObject *
|
|||
cfg_instr *inst = &bb->b_instr[i];
|
||||
if (inst->i_opcode == LOAD_CONST) {
|
||||
PyObject *constant = get_const_value(inst->i_opcode, inst->i_oparg, consts);
|
||||
if (constant == NULL) {
|
||||
return ERROR;
|
||||
}
|
||||
int res = maybe_instr_make_load_smallint(inst, constant, consts, const_cache);
|
||||
Py_DECREF(constant);
|
||||
if (res < 0) {
|
||||
|
|
@ -4073,6 +4084,10 @@ _PyCompile_OptimizeCfg(PyObject *seq, PyObject *consts, int nlocals)
|
|||
PyErr_SetString(PyExc_ValueError, "expected an instruction sequence");
|
||||
return NULL;
|
||||
}
|
||||
if (!PyList_Check(consts)) {
|
||||
PyErr_SetString(PyExc_TypeError, "consts must be a list");
|
||||
return NULL;
|
||||
}
|
||||
PyObject *const_cache = PyDict_New();
|
||||
if (const_cache == NULL) {
|
||||
return NULL;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue