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

Skip to content

Commit 3155db3

Browse files
committed
setup_confname_table(): Close memory leak caused by not decref'ing the
inserted dictionary values. Also, simplified the logic a bit.
1 parent 8deeced commit 3155db3

1 file changed

Lines changed: 15 additions & 16 deletions

File tree

Modules/posixmodule.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4325,27 +4325,26 @@ setup_confname_table(table, tablesize, tablename, moddict)
43254325
PyObject *moddict;
43264326
{
43274327
PyObject *d = NULL;
4328+
size_t i;
4329+
int status;
43284330

43294331
qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
43304332
d = PyDict_New();
4331-
if (d != NULL) {
4332-
PyObject *o;
4333-
size_t i = 0;
4334-
4335-
for (; i < tablesize; ++i) {
4336-
o = PyInt_FromLong(table[i].value);
4337-
if (o == NULL
4338-
|| PyDict_SetItemString(d, table[i].name, o) == -1) {
4339-
Py_DECREF(d);
4340-
d = NULL;
4341-
return -1;
4333+
if (d == NULL)
4334+
return -1;
4335+
4336+
for (i=0; i < tablesize; ++i) {
4337+
PyObject *o = PyInt_FromLong(table[i].value);
4338+
if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
4339+
Py_XDECREF(o);
4340+
Py_DECREF(d);
4341+
return -1;
43424342
}
4343-
}
4344-
if (PyDict_SetItemString(moddict, tablename, d) == -1)
4345-
return -1;
4346-
return 0;
4343+
Py_DECREF(o);
43474344
}
4348-
return -1;
4345+
status = PyDict_SetItemString(moddict, tablename, d);
4346+
Py_DECREF(d);
4347+
return status;
43494348
}
43504349

43514350
/* Return -1 on failure, 0 on success. */

0 commit comments

Comments
 (0)