Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
69 changes: 69 additions & 0 deletions Lib/test/test_ordered_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import sys
import unittest
import weakref

Comment thread
fatelei marked this conversation as resolved.
Outdated
from collections.abc import MutableMapping
from test import mapping_tests, support
from test.support import import_helper
Expand Down Expand Up @@ -970,6 +971,74 @@ def test_weakref_list_is_not_traversed(self):

gc.collect()

def test_copy_concurrent_clear_in__getitem__(self):
Comment thread
fatelei marked this conversation as resolved.
class MyOD(self.OrderedDict):
def __getitem__(self, key):
super().clear()
return None

od = MyOD([(i, i) for i in range(4)])
Comment thread
fatelei marked this conversation as resolved.
Outdated
self.assertRaises(RuntimeError, od.copy)

def test_copy_concurrent_insertion_in__getitem__(self):
class MyOD(self.OrderedDict):
def __getitem__(self, key):
self['new_key'] = 'new_value'
return super().__getitem__(key)

od = MyOD([(1, 'one'), (2, 'two')])
self.assertRaises(RuntimeError, od.copy)

def test_copy_concurrent_deletion_by_del_in__getitem__(self):
class MyOD(self.OrderedDict):
call_count = 0
def __getitem__(self, key):
MyOD.call_count += 1
Comment thread
fatelei marked this conversation as resolved.
Outdated
if MyOD.call_count == 1:
del self[3]
return super().__getitem__(key)

od = MyOD([(1, 'one'), (2, 'two'), (3, 'three')])
self.assertRaises(RuntimeError, od.copy)

def test_copy_concurrent_deletion_by_pop_in__getitem__(self):
class MyOD(self.OrderedDict):
call_count = 0
def __getitem__(self, key):
MyOD.call_count += 1
Comment thread
fatelei marked this conversation as resolved.
Outdated
if MyOD.call_count == 1:
self.pop(3, None)
Comment thread
fatelei marked this conversation as resolved.
Outdated
return super().__getitem__(key)

od = MyOD([(1, 'one'), (2, 'two'), (3, 'three')])
self.assertRaises(RuntimeError, od.copy)

def test_copy_concurrent_deletion_and_set_in__getitem__(self):
class MyOD(self.OrderedDict):
call_count = 0
def __getitem__(self, key):
MyOD.call_count += 1
Comment thread
fatelei marked this conversation as resolved.
Outdated
if MyOD.call_count == 1:
del self[3]
elif MyOD.call_count == 2:
self['new_key'] = 'new_value'
return super().__getitem__(key)
Comment thread
fatelei marked this conversation as resolved.

od = MyOD([(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')])
self.assertRaises(RuntimeError, od.copy)

def test_copy_concurrent_mutation_in__setitem__(self):
od = None
Comment thread
fatelei marked this conversation as resolved.
Outdated
Comment thread
fatelei marked this conversation as resolved.
Outdated

class MyOD(self.OrderedDict):
def __setitem__(self, key, value):
if od is not None and len(od) > 1:
Comment thread
fatelei marked this conversation as resolved.
Outdated
del od[3]
return super().__setitem__(key, value)

od = MyOD([(1, 'one'), (2, 'two'), (3, 'three')])
self.assertRaises(RuntimeError, od.copy)


class PurePythonOrderedDictSubclassTests(PurePythonOrderedDictTests):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:mod:`collections`: fix use-after-free crashes in :meth:`OrderedDict.copy <dict.copy>` when the dictionary to copy is concurrently mutated.
28 changes: 20 additions & 8 deletions Objects/odictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1266,21 +1266,33 @@ OrderedDict_copy_impl(PyObject *od)
}
}
else {
_odict_FOREACH(od, node) {
int res;
PyObject *value = PyObject_GetItem((PyObject *)od,
_odictnode_KEY(node));
if (value == NULL)
PyODictObject *self = _PyODictObject_CAST(od);
size_t state = self->od_state;
_ODictNode *cur;
Comment thread
fatelei marked this conversation as resolved.
Outdated

_odict_FOREACH(od, cur) {
PyObject *key = Py_NewRef(_odictnode_KEY(cur));
PyObject *value = PyObject_GetItem(od, key);
if (value == NULL) {
Py_DECREF(key);
goto fail;
res = PyObject_SetItem((PyObject *)od_copy,
_odictnode_KEY(node), value);
}
Comment thread
fatelei marked this conversation as resolved.
Comment thread
fatelei marked this conversation as resolved.
int rc = PyObject_SetItem(od_copy, key, value);
Comment thread
fatelei marked this conversation as resolved.
Py_DECREF(key);
Py_DECREF(value);
if (res != 0)
if (rc != 0) {
goto fail;
}
if (self->od_state != state) {
goto invalid_state;
}
}
}
return od_copy;

invalid_state:
PyErr_SetString(PyExc_RuntimeError,
"OrderedDict mutated during iteration");
fail:
Py_DECREF(od_copy);
return NULL;
Expand Down
Loading