]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-111654: remove redundant decref in LOAD_FROM_DICT_OR_DEREF (#111655)
authorAN Long <aisk@users.noreply.github.com>
Fri, 3 Nov 2023 04:06:51 +0000 (12:06 +0800)
committerGitHub <noreply@github.com>
Fri, 3 Nov 2023 04:06:51 +0000 (21:06 -0700)
Lib/test/test_exceptions.py
Misc/NEWS.d/next/Core and Builtins/2023-11-03-01-04-55.gh-issue-111654.scUhDO.rst [new file with mode: 0644]
Python/bytecodes.c
Python/executor_cases.c.h
Python/generated_cases.c.h

index 7f1d5ee9322b4589a898fb1f1a101a7c1f380ba0..8ccf08703e5389f52a45c6ecc3c3cfd9a733e7cd 100644 (file)
@@ -1844,6 +1844,13 @@ class NameErrorTests(unittest.TestCase):
         self.assertIn("nonsense", err.getvalue())
         self.assertIn("ZeroDivisionError", err.getvalue())
 
+    def test_gh_111654(self):
+        def f():
+            class TestClass:
+                TestClass
+
+        self.assertRaises(NameError, f)
+
     # Note: name suggestion tests live in `test_traceback`.
 
 
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-11-03-01-04-55.gh-issue-111654.scUhDO.rst b/Misc/NEWS.d/next/Core and Builtins/2023-11-03-01-04-55.gh-issue-111654.scUhDO.rst
new file mode 100644 (file)
index 0000000..e9a896e
--- /dev/null
@@ -0,0 +1,2 @@
+Fix runtime crash when some error happens in opcode
+``LOAD_FROM_DICT_OR_DEREF``.
index 9f1dfa3b7231b52f06245dff8acee3b8f909410b..ddaae32f43809bf90130f824efb710483734751f 100644 (file)
@@ -1535,10 +1535,8 @@ dummy_func(
             assert(oparg >= 0 && oparg < _PyFrame_GetCode(frame)->co_nlocalsplus);
             name = PyTuple_GET_ITEM(_PyFrame_GetCode(frame)->co_localsplusnames, oparg);
             if (PyMapping_GetOptionalItem(class_dict, name, &value) < 0) {
-                Py_DECREF(class_dict);
                 GOTO_ERROR(error);
             }
-            Py_DECREF(class_dict);
             if (!value) {
                 PyObject *cell = GETLOCAL(oparg);
                 value = PyCell_GET(cell);
@@ -1548,6 +1546,7 @@ dummy_func(
                 }
                 Py_INCREF(value);
             }
+            Py_DECREF(class_dict);
         }
 
         inst(LOAD_DEREF, ( -- value)) {
index 903fc40595ceb7f1e4001bb6255bb48cedf94366..eb56c34b4327832bbd7e1f83bf1cdd5b56715e7d 100644 (file)
             assert(oparg >= 0 && oparg < _PyFrame_GetCode(frame)->co_nlocalsplus);
             name = PyTuple_GET_ITEM(_PyFrame_GetCode(frame)->co_localsplusnames, oparg);
             if (PyMapping_GetOptionalItem(class_dict, name, &value) < 0) {
-                Py_DECREF(class_dict);
                 GOTO_ERROR(error);
             }
-            Py_DECREF(class_dict);
             if (!value) {
                 PyObject *cell = GETLOCAL(oparg);
                 value = PyCell_GET(cell);
                 }
                 Py_INCREF(value);
             }
+            Py_DECREF(class_dict);
             stack_pointer[-1] = value;
             break;
         }
index 38c368fcc102003ab5846d54e89f29a8e389861a..5c3a579f79ae029938cb7be00738f9361346ddbf 100644 (file)
             assert(oparg >= 0 && oparg < _PyFrame_GetCode(frame)->co_nlocalsplus);
             name = PyTuple_GET_ITEM(_PyFrame_GetCode(frame)->co_localsplusnames, oparg);
             if (PyMapping_GetOptionalItem(class_dict, name, &value) < 0) {
-                Py_DECREF(class_dict);
                 GOTO_ERROR(error);
             }
-            Py_DECREF(class_dict);
             if (!value) {
                 PyObject *cell = GETLOCAL(oparg);
                 value = PyCell_GET(cell);
                 }
                 Py_INCREF(value);
             }
+            Py_DECREF(class_dict);
             stack_pointer[-1] = value;
             DISPATCH();
         }