Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-141805: avoid increase the set->used twice when the __eq__ trigger another add #142007

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

Closed
kemingy wants to merge 6 commits into python:main from kemingy:gh-141805
Closed
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
29 changes: 29 additions & 0 deletions Lib/test/test_set.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,35 @@ def __hash__(self):
with self.assertRaises(KeyError):
myset.discard(elem2)

def test_set_add_to_dummy_slot(self):
# gh-141805
tasks = set()

class Dummy:
def __hash__(self):
return 0

class CorruptTrigger:
triggered = False

def __hash__(self):
return 0

def __eq__(self, value):
if not self.triggered:
self.triggered = True
tasks.add(self)
return False

tasks.add(Dummy())
tasks.add(Dummy())
tasks.pop()
self.assertEqual(len(tasks), 1)
tasks.add(CorruptTrigger())
self.assertEqual(len(tasks), 2)
# cover the case in gh-141805 that triggers segfault
repr(tasks)


class SetSubclass(set):
pass
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix set corruption due to invalid length calculation when set additions occur
in the element comparison (:meth:`object.__eq__`) methods.
1 change: 1 addition & 0 deletions Objects/setobject.c
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ set_add_entry_takeref(PySetObject *so, PyObject *key, Py_hash_t hash)
else if (entry->hash == -1) {
assert (entry->key == dummy);
freeslot = entry;
goto found_unused_or_dummy;
Copy link
Member

@serhiy-storchaka serhiy-storchaka Jan 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is incorrect.

Imagine you added two elements with the same hash, A and B. Then A hes been removed, and replaced with a dummy (this is why dummy is needed). Then we try to add B again. With your code, it will find a dummy in place of A, and replace it with B. Now you have two Bs in a set.

Copy link
Member

@serhiy-storchaka serhiy-storchaka Jan 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#143781 adds a test for this.

}
entry++;
} while (probes--);
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /