Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-102250: Fix double-decref in COMPARE_AND_BRANCH error case #102287

Merged
merged 2 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Lib/test/test_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,26 @@ def __len__(self):
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a segfault occurring when the interpreter calls a ``__bool__`` method that raises.
4 changes: 1 addition & 3 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 1 addition & 3 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.