Skip to content

Commit e3c3f9f

Browse files
authored
gh-102250: Fix double-decref in COMPARE_AND_BRANCH error case (GH-102287)
1 parent 101a12c commit e3c3f9f

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

Lib/test/test_bool.py

+20
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,26 @@ def __len__(self):
319319
return -1
320320
self.assertRaises(ValueError, bool, Eggs())
321321

322+
def test_interpreter_convert_to_bool_raises(self):
323+
class SymbolicBool:
324+
def __bool__(self):
325+
raise TypeError
326+
327+
class Symbol:
328+
def __gt__(self, other):
329+
return SymbolicBool()
330+
331+
x = Symbol()
332+
333+
with self.assertRaises(TypeError):
334+
if x > 0:
335+
msg = "x > 0 was true"
336+
else:
337+
msg = "x > 0 was false"
338+
339+
# This used to create negative refcounts, see gh-102250
340+
del x
341+
322342
def test_from_bytes(self):
323343
self.assertIs(bool.from_bytes(b'\x00'*8, 'big'), False)
324344
self.assertIs(bool.from_bytes(b'abcd', 'little'), True)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed a segfault occurring when the interpreter calls a ``__bool__`` method that raises.

Python/bytecodes.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -1754,9 +1754,7 @@ dummy_func(
17541754
int offset = next_instr[1].op.arg;
17551755
int err = PyObject_IsTrue(cond);
17561756
Py_DECREF(cond);
1757-
if (err < 0) {
1758-
goto error;
1759-
}
1757+
ERROR_IF(err < 0, error);
17601758
if (jump_on_true == (err != 0)) {
17611759
JUMPBY(offset);
17621760
}

Python/generated_cases.c.h

+1-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)