Skip to content
Open
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
15 changes: 15 additions & 0 deletions Lib/test/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,21 @@ def __init__(self, x, y):
self.assertEqual(list(reversed(A(1, 0).__dict__)), ['x'])
self.assertEqual(list(reversed(A(0, 1).__dict__)), ['y'])

def test_reversed_dict_after_clear_and_restore(self):
d = {}
for i in range(1000):
d[f"k{i}"] = i

for i in range(1, 1000):
del d[f"k{i}"]

it = reversed(d)

d.clear()
d["k0"] = 0

self.assertEqual(list(it), [])

def test_dict_copy_order(self):
# bpo-34320
od = collections.OrderedDict([('a', 1), ('b', 2)])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix an out-of-bounds access in reverse dictionary iterators when the
underlying dictionary is cleared and modified after the iterator is created.
10 changes: 10 additions & 0 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6272,13 +6272,23 @@ dictreviter_iter_lock_held(PyDictObject *d, PyObject *self)
if (i < 0) {
goto fail;
}

if (_PyDict_HasSplitTable(d)) {
if (i >= d->ma_used) {
Comment thread
BHUVANSH855 marked this conversation as resolved.
goto fail;
}

int index = get_index_from_order(d, i);
key = LOAD_SHARED_KEY(DK_UNICODE_ENTRIES(k)[index].me_key);
value = d->ma_values->values[index];
assert (value != NULL);
}
else {
Py_ssize_t n = k->dk_nentries;
if (i >= n) {
goto fail;
}

if (DK_IS_UNICODE(k)) {
PyDictUnicodeEntry *entry_ptr = &DK_UNICODE_ENTRIES(k)[i];
while (entry_ptr->me_value == NULL) {
Expand Down
Loading