]> granicus.if.org Git - python/commitdiff
setup_confname_table(): Close memory leak caused by not decref'ing the
authorBarry Warsaw <barry@python.org>
Thu, 13 Apr 2000 15:20:40 +0000 (15:20 +0000)
committerBarry Warsaw <barry@python.org>
Thu, 13 Apr 2000 15:20:40 +0000 (15:20 +0000)
inserted dictionary values.  Also, simplified the logic a bit.

Modules/posixmodule.c

index 0ce2dfac94d42a45f91667433c477d974ff752a9..c948b952e13f17200e8a298ae02b78a99b06e28c 100644 (file)
@@ -4325,27 +4325,26 @@ setup_confname_table(table, tablesize, tablename, moddict)
      PyObject *moddict;
 {
     PyObject *d = NULL;
+    size_t i;
+    int status;
 
     qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
     d = PyDict_New();
-    if (d != NULL) {
-        PyObject *o;
-        size_t i = 0;
-
-        for (; i < tablesize; ++i) {
-            o = PyInt_FromLong(table[i].value);
-            if (o == NULL
-                || PyDict_SetItemString(d, table[i].name, o) == -1) {
-                Py_DECREF(d);
-                d = NULL;
-                return -1;
+    if (d == NULL)
+           return -1;
+
+    for (i=0; i < tablesize; ++i) {
+            PyObject *o = PyInt_FromLong(table[i].value);
+            if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
+                   Py_XDECREF(o);
+                   Py_DECREF(d);
+                   return -1;
             }
-        }
-        if (PyDict_SetItemString(moddict, tablename, d) == -1)
-            return -1;
-        return 0;
+           Py_DECREF(o);
     }
-    return -1;
+    status = PyDict_SetItemString(moddict, tablename, d);
+    Py_DECREF(d);
+    return status;
 }
 
 /* Return -1 on failure, 0 on success. */