]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46841: Don't scan backwards in bytecode (GH-31901)
authorMark Shannon <mark@hotpy.org>
Wed, 16 Mar 2022 00:08:37 +0000 (00:08 +0000)
committerGitHub <noreply@github.com>
Wed, 16 Mar 2022 00:08:37 +0000 (17:08 -0700)
Objects/genobject.c
Python/ceval.c

index 4fac0ce241c0968123f635903f3438febdfdfa64..6551b939c4590c4463a100dfc36f251f04a832f0 100644 (file)
@@ -359,9 +359,12 @@ _PyGen_yf(PyGenObject *gen)
             assert(code[0] != SEND);
             return NULL;
         }
-
-        if (code[(frame->f_lasti-1)*sizeof(_Py_CODEUNIT)] != SEND || frame->stacktop < 0)
+        int opcode = code[(frame->f_lasti+1)*sizeof(_Py_CODEUNIT)];
+        int oparg = code[(frame->f_lasti+1)*sizeof(_Py_CODEUNIT)+1];
+        if (opcode != RESUME || oparg < 2) {
+            /* Not in a yield from */
             return NULL;
+        }
         yf = _PyFrame_StackPeek(frame);
         Py_INCREF(yf);
     }
index d0fc31ec6c5d83ae1260cd7486c1ba3a6d8196b4..81759ad770b1e3f392c49ddfcdb395633b298f49 100644 (file)
@@ -1565,16 +1565,6 @@ trace_function_exit(PyThreadState *tstate, _PyInterpreterFrame *frame, PyObject
     return 0;
 }
 
-static int
-skip_backwards_over_extended_args(PyCodeObject *code, int offset)
-{
-    _Py_CODEUNIT *instrs = (_Py_CODEUNIT *)PyBytes_AS_STRING(code->co_code);
-    while (offset > 0 && _Py_OPCODE(instrs[offset-1]) == EXTENDED_ARG) {
-        offset--;
-    }
-    return offset;
-}
-
 static _PyInterpreterFrame *
 pop_frame(PyThreadState *tstate, _PyInterpreterFrame *frame)
 {
@@ -5445,7 +5435,7 @@ handle_eval_breaker:
 #endif
     {
         if (tstate->tracing == 0) {
-            int instr_prev = skip_backwards_over_extended_args(frame->f_code, frame->f_lasti);
+            int instr_prev = frame->f_lasti;
             frame->f_lasti = INSTR_OFFSET();
             TRACING_NEXTOPARG();
             if (opcode == RESUME) {
@@ -6737,9 +6727,13 @@ maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
        then call the trace function if we're tracing source lines.
     */
     initialize_trace_info(&tstate->trace_info, frame);
-    _Py_CODEUNIT prev = ((_Py_CODEUNIT *)PyBytes_AS_STRING(frame->f_code->co_code))[instr_prev];
+    int entry_point = 0;
+    _Py_CODEUNIT *code = (_Py_CODEUNIT *)PyBytes_AS_STRING(frame->f_code->co_code);
+    while (_Py_OPCODE(code[entry_point]) != RESUME) {
+        entry_point++;
+    }
     int lastline;
-    if (_Py_OPCODE(prev) == RESUME && _Py_OPARG(prev) == 0) {
+    if (instr_prev <= entry_point) {
         lastline = -1;
     }
     else {