]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-102250: Fix double-decref in COMPARE_AND_BRANCH error case (GH-102287)
authorDennis Sweeney <36520290+sweeneyde@users.noreply.github.com>
Mon, 27 Feb 2023 10:46:40 +0000 (05:46 -0500)
committerGitHub <noreply@github.com>
Mon, 27 Feb 2023 10:46:40 +0000 (10:46 +0000)
Lib/test/test_bool.py
Misc/NEWS.d/next/Core and Builtins/2023-02-26-23-10-32.gh-issue-102250.7MUKoC.rst [new file with mode: 0644]
Python/bytecodes.c
Python/generated_cases.c.h

index b711ffb9a3ecd53d65405a8000c88435ec8fa1df..916e22a527a8e0600638c7733e49c3e5f4d1c9ae 100644 (file)
@@ -319,6 +319,26 @@ class BoolTest(unittest.TestCase):
                 return -1
         self.assertRaises(ValueError, bool, Eggs())
 
+    def test_interpreter_convert_to_bool_raises(self):
+        class SymbolicBool:
+            def __bool__(self):
+                raise TypeError
+
+        class Symbol:
+            def __gt__(self, other):
+                return SymbolicBool()
+
+        x = Symbol()
+
+        with self.assertRaises(TypeError):
+            if x > 0:
+                msg = "x > 0 was true"
+            else:
+                msg = "x > 0 was false"
+
+        # This used to create negative refcounts, see gh-102250
+        del x
+
     def test_from_bytes(self):
         self.assertIs(bool.from_bytes(b'\x00'*8, 'big'), False)
         self.assertIs(bool.from_bytes(b'abcd', 'little'), True)
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-02-26-23-10-32.gh-issue-102250.7MUKoC.rst b/Misc/NEWS.d/next/Core and Builtins/2023-02-26-23-10-32.gh-issue-102250.7MUKoC.rst
new file mode 100644 (file)
index 0000000..17ab0cd
--- /dev/null
@@ -0,0 +1 @@
+Fixed a segfault occurring when the interpreter calls a ``__bool__`` method that raises.
index ad68c794fe7acb0257709e2a98a8fefec7e84f9b..7e9b36f697210aa0a3d1104348d2b66a964d123c 100644 (file)
@@ -1754,9 +1754,7 @@ dummy_func(
             int offset = next_instr[1].op.arg;
             int err = PyObject_IsTrue(cond);
             Py_DECREF(cond);
-            if (err < 0) {
-                goto error;
-            }
+            ERROR_IF(err < 0, error);
             if (jump_on_true == (err != 0)) {
                 JUMPBY(offset);
             }
index 2987adc3bba566e4b4300666b2be71e1599972b7..271ba26f489521506fc9952ced1a17a8c0b85650 100644 (file)
             int offset = next_instr[1].op.arg;
             int err = PyObject_IsTrue(cond);
             Py_DECREF(cond);
-            if (err < 0) {
-                goto error;
-            }
+            if (err < 0) goto pop_2_error;
             if (jump_on_true == (err != 0)) {
                 JUMPBY(offset);
             }