diff options
author | Gregory P. Smith <greg@krypto.org> | 2018-11-08 23:30:36 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-08 23:30:36 -0800 |
commit | 65e1a1fd311943866361fcb288c0df65dadbe092 (patch) | |
tree | 89bf7c842b2363d44a04dec1cb507f77429e53f8 /Python/peephole.c | |
parent | 11a33e171b89ea9267672b0664d739ca06e459ca (diff) | |
download | cpython-git-65e1a1fd311943866361fcb288c0df65dadbe092.tar.gz |
bpo-35193: Fix an off by one error in the RETURN_VALUE case. (GH-10418) (GH-10422)
Fix an off by one error in the peephole optimizer when checking for unreachable code beyond a return.
Do a bounds check within find_op so it can return before going past the end as a safety measure.
https://github.com/python/cpython/commit/7db3c488335168993689ddae5914a28e16188447#diff-a33329ae6ae0bb295d742f0caf93c137
introduced this off by one error while fixing another one nearby.
This bug was shipped in all Python 3.6 and 3.7 releases.
The included unittest won't fail unless you do a clang msan build.
(cherry picked from commit 49fa4a9f1ef387e16596f271414c855339eadf09)
Diffstat (limited to 'Python/peephole.c')
-rw-r--r-- | Python/peephole.c | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/Python/peephole.c b/Python/peephole.c index 31d4e92cfd..3fa3b7fcee 100644 --- a/Python/peephole.c +++ b/Python/peephole.c @@ -96,9 +96,9 @@ lastn_const_start(const _Py_CODEUNIT *codestr, Py_ssize_t i, Py_ssize_t n) /* Scans through EXTENDED ARGs, seeking the index of the effective opcode */ static Py_ssize_t -find_op(const _Py_CODEUNIT *codestr, Py_ssize_t i) +find_op(const _Py_CODEUNIT *codestr, Py_ssize_t codelen, Py_ssize_t i) { - while (_Py_OPCODE(codestr[i]) == EXTENDED_ARG) { + while (i < codelen && _Py_OPCODE(codestr[i]) == EXTENDED_ARG) { i++; } return i; @@ -590,7 +590,7 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names, CONST_STACK_CREATE(); - for (i=find_op(codestr, 0) ; i<codelen ; i=nexti) { + for (i=find_op(codestr, codelen, 0) ; i<codelen ; i=nexti) { opcode = _Py_OPCODE(codestr[i]); op_start = i; while (op_start >= 1 && _Py_OPCODE(codestr[op_start-1]) == EXTENDED_ARG) { @@ -755,7 +755,7 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names, case JUMP_IF_FALSE_OR_POP: case JUMP_IF_TRUE_OR_POP: h = get_arg(codestr, i) / sizeof(_Py_CODEUNIT); - tgt = find_op(codestr, h); + tgt = find_op(codestr, codelen, h); j = _Py_OPCODE(codestr[tgt]); if (CONDITIONAL_JUMP(j)) { @@ -796,7 +796,7 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names, case SETUP_WITH: case SETUP_ASYNC_WITH: h = GETJUMPTGT(codestr, i); - tgt = find_op(codestr, h); + tgt = find_op(codestr, codelen, h); /* Replace JUMP_* to a RETURN into just a RETURN */ if (UNCONDITIONAL_JUMP(opcode) && _Py_OPCODE(codestr[tgt]) == RETURN_VALUE) { @@ -825,7 +825,7 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names, } if (h > i + 1) { fill_nops(codestr, i + 1, h); - nexti = find_op(codestr, h); + nexti = find_op(codestr, codelen, h); } break; } |