Skip to content

Commit ca30b02

Browse files
committed
Issue python#24992: Fix error handling and a race condition (related to garbage
collection) in collections.OrderedDict constructor. Patch reviewed by Serhiy Storchaka.
1 parent 38b8ae0 commit ca30b02

2 files changed

Lines changed: 25 additions & 18 deletions

File tree

‎Misc/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Core and Builtins
1414
Library
1515
-------
1616

17+
- Issue #24992: Fix error handling and a race condition (related to garbage
18+
collection) in collections.OrderedDict constructor.
19+
1720
- Issue #24881: Fixed setting binary mode in Python implementation of FileIO
1821
on Windows and Cygwin. Patch from Akira Li.
1922

‎Objects/odictobject.c‎

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ For removing nodes:
9898
9999
Others:
100100
101-
* _odict_initialize(od)
102101
* _odict_find_node(od, key)
103102
* _odict_keys_equal(od1, od2)
104103
@@ -602,15 +601,6 @@ _odict_get_index(PyODictObject *od, PyObject *key)
602601
return _odict_get_index_hash(od, key, hash);
603602
}
604603

605-
static int
606-
_odict_initialize(PyODictObject *od)
607-
{
608-
od->od_state = 0;
609-
_odict_FIRST(od) = NULL;
610-
_odict_LAST(od) = NULL;
611-
return _odict_resize((PyODictObject *)od);
612-
}
613-
614604
/* Returns NULL if there was some error or the key was not found. */
615605
static _ODictNode *
616606
_odict_find_node(PyODictObject *od, PyObject *key)
@@ -744,7 +734,7 @@ _odict_pop_node(PyODictObject *od, _ODictNode *node, PyObject *key)
744734
/* If someone calls PyDict_DelItem() directly on an OrderedDict, we'll
745735
get all sorts of problems here. In PyODict_DelItem we make sure to
746736
call _odict_clear_node first.
747-
737+
748738
This matters in the case of colliding keys. Suppose we add 3 keys:
749739
[A, B, C], where the hash of C collides with A and the next possible
750740
index in the hash table is occupied by B. If we remove B then for C
@@ -1739,14 +1729,28 @@ odict_init(PyObject *self, PyObject *args, PyObject *kwds)
17391729
static PyObject *
17401730
odict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
17411731
{
1742-
PyObject *od = PyDict_Type.tp_new(type, args, kwds);
1743-
if (od != NULL) {
1744-
if (_odict_initialize((PyODictObject *)od) < 0)
1745-
return NULL;
1746-
((PyODictObject *)od)->od_inst_dict = PyDict_New();
1747-
((PyODictObject *)od)->od_weakreflist = NULL;
1732+
PyObject *dict;
1733+
PyODictObject *od;
1734+
1735+
dict = PyDict_New();
1736+
if (dict == NULL)
1737+
return NULL;
1738+
1739+
od = (PyODictObject *)PyDict_Type.tp_new(type, args, kwds);
1740+
if (od == NULL) {
1741+
Py_DECREF(dict);
1742+
return NULL;
17481743
}
1749-
return od;
1744+
1745+
od->od_inst_dict = dict;
1746+
/* type constructor fills the memory with zeros (see
1747+
PyType_GenericAlloc()), there is no need to set them to zero again */
1748+
if (_odict_resize(od) < 0) {
1749+
Py_DECREF(od);
1750+
return NULL;
1751+
}
1752+
1753+
return (PyObject*)od;
17501754
}
17511755

17521756
/* PyODict_Type */

0 commit comments

Comments
 (0)