Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e3dfa9e

Browse files
Issue #16991: Fix a few leaks and other memory-related concerns in OrderedDict.
1 parent d0a0645 commit e3dfa9e

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

Objects/odictobject.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,8 +1030,7 @@ odict_reduce(register PyODictObject *od)
10301030
goto Done;
10311031
if (!ns_len) {
10321032
/* nothing novel to pickle in od.__dict__ */
1033-
Py_DECREF(ns);
1034-
ns = NULL;
1033+
Py_CLEAR(ns);
10351034
}
10361035
}
10371036

@@ -1184,8 +1183,7 @@ _odict_popkey(PyObject *od, PyObject *key, PyObject *failobj)
11841183
value = PyObject_GetItem(od, key);
11851184
if (value != NULL) {
11861185
if (PyObject_DelItem(od, key) == -1) {
1187-
Py_DECREF(value);
1188-
value = NULL;
1186+
Py_CLEAR(value);
11891187
}
11901188
}
11911189
}
@@ -1718,10 +1716,10 @@ odict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
17181716
{
17191717
PyObject *od = PyDict_Type.tp_new(type, args, kwds);
17201718
if (od != NULL) {
1721-
((PyODictObject *)od)->od_inst_dict = PyDict_New();
1722-
((PyODictObject *)od)->od_weakreflist = NULL;
17231719
if (_odict_initialize((PyODictObject *)od) < 0)
17241720
return NULL;
1721+
((PyODictObject *)od)->od_inst_dict = PyDict_New();
1722+
((PyODictObject *)od)->od_weakreflist = NULL;
17251723
}
17261724
return od;
17271725
}
@@ -1845,8 +1843,7 @@ odictiter_nextkey(odictiterobject *di)
18451843
node = _odict_find_node(di->di_odict, di->di_current);
18461844
if (node == NULL) {
18471845
/* Must have been deleted. */
1848-
Py_DECREF(di->di_current);
1849-
di->di_current = NULL;
1846+
Py_CLEAR(di->di_current);
18501847
return NULL;
18511848
}
18521849
key = di->di_current;
@@ -1884,8 +1881,11 @@ odictiter_iternext(odictiterobject *di)
18841881
PyObject *result = di->di_result;
18851882

18861883
value = PyODict_GetItem((PyObject *)di->di_odict, key); /* borrowed */
1887-
if (value == NULL)
1884+
if (value == NULL) {
1885+
Py_DECREF(key);
18881886
return NULL;
1887+
}
1888+
Py_INCREF(value);
18891889

18901890
if (result->ob_refcnt == 1) {
18911891
/* not in use so we can reuse it
@@ -1896,11 +1896,13 @@ odictiter_iternext(odictiterobject *di)
18961896
}
18971897
else {
18981898
result = PyTuple_New(2);
1899-
if (result == NULL)
1899+
if (result == NULL) {
1900+
Py_DECREF(key);
1901+
Py_DECREF(value);
19001902
return NULL;
1903+
}
19011904
}
19021905

1903-
Py_INCREF(value);
19041906
PyTuple_SET_ITEM(result, 0, key); /* steals reference */
19051907
PyTuple_SET_ITEM(result, 1, value); /* steals reference */
19061908

@@ -2365,7 +2367,6 @@ mutablemapping_update(PyObject *self, PyObject *args, PyObject *kwargs)
23652367
else if (PyObject_HasAttrString(other, "keys")) { /* never fails */
23662368
PyObject *keys, *iterator, *key;
23672369
keys = PyObject_CallMethod(other, "keys", NULL);
2368-
Py_DECREF(other);
23692370
if (keys == NULL)
23702371
return NULL;
23712372
iterator = PyObject_GetIter(keys);
@@ -2383,6 +2384,7 @@ mutablemapping_update(PyObject *self, PyObject *args, PyObject *kwargs)
23832384
}
23842385
Py_DECREF(key);
23852386
}
2387+
Py_DECREF(other);
23862388
Py_DECREF(iterator);
23872389
if (res != 0 || PyErr_Occurred())
23882390
return NULL;

0 commit comments

Comments
 (0)