Cleanup (decref) some objects added to list.
authorNoel Power <noel.power@suse.com>
Wed, 23 Jan 2019 18:43:43 +0000 (18:43 +0000)
committerNoel Power <npower@samba.org>
Thu, 7 Feb 2019 12:44:30 +0000 (13:44 +0100)
PyList_Append doesn't steal references, so if the item created is
a temp object, created just to be added to the list we need to
 decref the item appended in order for it to be released.

Signed-off-by: Noel Power <noel.power@suse.com>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
lib/ldb/pyldb.c
source3/libsmb/pylibsmb.c
source3/passdb/py_passdb.c
source4/lib/policy/pypolicy.c

index 1d4fe59f1031a60301532f772c1579227406a37b..3deb393e467c41cb6a6f6d8fdc614bae91699830 100644 (file)
@@ -1741,7 +1741,21 @@ static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args)
                ldif = ldb_ldif_read_string(self->ldb_ctx, &s);
                talloc_steal(mem_ctx, ldif);
                if (ldif) {
-                       PyList_Append(list, ldb_ldif_to_pyobject(ldif));
+                       int res = 0;
+                       PyObject *py_ldif = ldb_ldif_to_pyobject(ldif);
+                       if (py_ldif == NULL) {
+                               Py_CLEAR(list);
+                               PyErr_BadArgument();
+                               talloc_free(mem_ctx);
+                               return NULL;
+                       }
+                       res = PyList_Append(list, py_ldif);
+                       Py_CLEAR(py_ldif);
+                       if (res == -1) {
+                               Py_CLEAR(list);
+                               talloc_free(mem_ctx);
+                               return NULL;
+                       }
                        last_dn = ldif->msg->dn;
                } else {
                        const char *last_dn_str = NULL;
@@ -1750,6 +1764,7 @@ static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args)
                                PyErr_SetString(PyExc_ValueError,
                                                "unable to parse LDIF "
                                                "string at first chunk");
+                               Py_CLEAR(list);
                                talloc_free(mem_ctx);
                                return NULL;
                        }
@@ -1766,6 +1781,7 @@ static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args)
                        PyErr_SetString(PyExc_ValueError,
                                        err_string);
                        talloc_free(mem_ctx);
+                       Py_CLEAR(list);
                        return NULL;
                }
        }
@@ -2201,8 +2217,24 @@ static PyObject *py_ldb_modules(PyLdbObject *self)
        PyObject *ret = PyList_New(0);
        struct ldb_module *mod;
 
+       if (ret == NULL) {
+               return PyErr_NoMemory();
+       }
        for (mod = ldb->modules; mod; mod = mod->next) {
-               PyList_Append(ret, PyLdbModule_FromModule(mod));
+               PyObject *item = PyLdbModule_FromModule(mod);
+               int res = 0;
+               if (item == NULL) {
+                       PyErr_SetString(PyExc_RuntimeError,
+                               "Failed to load LdbModule");
+                       Py_CLEAR(ret);
+                       return NULL;
+               }
+               res = PyList_Append(ret, item);
+               Py_CLEAR(item);
+               if (res == -1) {
+                       Py_CLEAR(ret);
+                       return NULL;
+               }
        }
 
        return ret;
index d2a8e05a667c2804598131bd90e34bff0b42fb25..5f3e6a8c724ac1a681778925997485e443f04bcd 100644 (file)
@@ -1151,6 +1151,7 @@ static NTSTATUS list_helper(const char *mntpoint, struct file_info *finfo,
        }
 
        ret = PyList_Append(result, file);
+       Py_CLEAR(file);
        if (ret == -1) {
                return NT_STATUS_INTERNAL_ERROR;
        }
index 2ac2942a47fa3fb7b9a036f593d2d6ed0dddb093..66f0381722057a51305c6b569f52cd8fb51189c7 100644 (file)
@@ -2107,12 +2107,19 @@ static PyObject *py_pdb_enum_group_mapping(PyObject *self, PyObject *args)
        for(i=0; i<num_entries; i++) {
                py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
                if (py_group_map) {
+                       int res = 0;
                        group_map = pytalloc_get_ptr(py_group_map);
                        *group_map = *gmap[i];
                        talloc_steal(group_map, gmap[i]->nt_name);
                        talloc_steal(group_map, gmap[i]->comment);
 
-                       PyList_Append(py_gmap_list, py_group_map);
+                       res = PyList_Append(py_gmap_list, py_group_map);
+                       Py_CLEAR(py_group_map);
+                       if (res == -1) {
+                               Py_CLEAR(py_gmap_list);
+                               talloc_free(frame);
+                               return NULL;
+                       }
                }
        }
 
@@ -2165,8 +2172,18 @@ static PyObject *py_pdb_enum_group_members(PyObject *self, PyObject *args)
        domain_sid = get_global_sam_sid();
 
        for(i=0; i<num_members; i++) {
+               int res = 0;
+               PyObject *py_member_sid = NULL;
                member_sid = dom_sid_add_rid(frame, domain_sid, member_rids[i]);
-               PyList_Append(py_sid_list, pytalloc_steal(dom_sid_Type, member_sid));
+               py_member_sid = pytalloc_steal(dom_sid_Type, member_sid);
+               res = PyList_Append(py_sid_list,
+                                   py_member_sid);
+               Py_CLEAR(py_member_sid);
+               if (res == -1) {
+                       talloc_free(frame);
+                       Py_CLEAR(py_sid_list);
+                       return NULL;
+               }
        }
 
        talloc_free(frame);
@@ -2215,7 +2232,11 @@ static PyObject *py_pdb_enum_group_memberships(PyObject *self, PyObject *args)
        }
 
        for(i=0; i<num_groups; i++) {
-               PyList_Append(py_sid_list, pytalloc_steal(dom_sid_Type, dom_sid_dup(NULL, &user_group_sids[i])));
+               PyObject *py_sid =
+                       pytalloc_steal(dom_sid_Type,
+                                      dom_sid_dup(NULL, &user_group_sids[i]));
+               PyList_Append(py_sid_list, py_sid);
+               Py_CLEAR(py_sid);
        }
 
        talloc_free(frame);
@@ -2543,15 +2564,23 @@ static PyObject *py_pdb_enum_aliasmem(PyObject *self, PyObject *args)
        }
 
        for(i=0; i<num_members; i++) {
+               int res = 0;
                py_member_sid = pytalloc_new(struct dom_sid, dom_sid_Type);
                if (py_member_sid == NULL) {
                        PyErr_NoMemory();
+                       Py_CLEAR(py_member_list);
                        talloc_free(frame);
                        return NULL;
                }
                tmp_sid = pytalloc_get_ptr(py_member_sid);
                *tmp_sid = member_sid[i];
-               PyList_Append(py_member_list, py_member_sid);
+               res = PyList_Append(py_member_list, py_member_sid);
+               Py_CLEAR(py_member_sid);
+               if (res == -1) {
+                       Py_CLEAR(py_member_list);
+                       talloc_free(frame);
+                       return NULL;
+               }
        }
 
        talloc_free(frame);
@@ -2676,13 +2705,20 @@ static PyObject *py_pdb_search_users(PyObject *self, PyObject *args)
                if (py_dict == NULL) {
                        PyErr_NoMemory();
                } else {
+                       int res = 0;
                        PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
                        PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
                        PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
                        PyDict_SetItemString(py_dict, "account_name", PyStr_FromString(entry->account_name));
                        PyDict_SetItemString(py_dict, "fullname", PyStr_FromString(entry->fullname));
                        PyDict_SetItemString(py_dict, "description", PyStr_FromString(entry->description));
-                       PyList_Append(py_userlist, py_dict);
+                       res = PyList_Append(py_userlist, py_dict);
+                       Py_CLEAR(py_dict);
+                       if (res == -1) {
+                               Py_CLEAR(py_userlist);
+                               talloc_free(frame);
+                               return NULL;
+                       }
                }
        }
        search->search_end(search);
@@ -2734,13 +2770,20 @@ static PyObject *py_pdb_search_groups(PyObject *self, PyObject *unused)
                if (py_dict == NULL) {
                        PyErr_NoMemory();
                } else {
+                       int res = 0;
                        PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
                        PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
                        PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
                        PyDict_SetItemString(py_dict, "account_name", PyStr_FromString(entry->account_name));
                        PyDict_SetItemString(py_dict, "fullname", PyStr_FromString(entry->fullname));
                        PyDict_SetItemString(py_dict, "description", PyStr_FromString(entry->description));
-                       PyList_Append(py_grouplist, py_dict);
+                       res = PyList_Append(py_grouplist, py_dict);
+                       Py_CLEAR(py_dict);
+                       if (res == -1) {
+                               talloc_free(frame);
+                               Py_CLEAR(py_grouplist);
+                               return NULL;
+                       }
                }
        }
        search->search_end(search);
@@ -2805,13 +2848,20 @@ static PyObject *py_pdb_search_aliases(PyObject *self, PyObject *args)
                if (py_dict == NULL) {
                        PyErr_NoMemory();
                } else {
+                       int res = 0;
                        PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
                        PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
                        PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
                        PyDict_SetItemString(py_dict, "account_name", PyStr_FromString(entry->account_name));
                        PyDict_SetItemString(py_dict, "fullname", PyStr_FromString(entry->fullname));
                        PyDict_SetItemString(py_dict, "description", PyStr_FromString(entry->description));
-                       PyList_Append(py_aliaslist, py_dict);
+                       res = PyList_Append(py_aliaslist, py_dict);
+                       Py_CLEAR(py_dict);
+                       if (res == -1) {
+                               Py_CLEAR(py_aliaslist);
+                               talloc_free(frame);
+                               return NULL;
+                       }
                }
        }
        search->search_end(search);
@@ -3083,6 +3133,7 @@ static PyObject *py_pdb_enum_trusteddoms(PyObject *self, PyObject *unused)
        }
 
        for(i=0; i<num_domains; i++) {
+               int res = 0;
                py_dict = PyDict_New();
                if (py_dict) {
                        PyDict_SetItemString(py_dict, "name",
@@ -3091,7 +3142,13 @@ static PyObject *py_pdb_enum_trusteddoms(PyObject *self, PyObject *unused)
                                        pytalloc_steal(dom_sid_Type, &domains[i]->sid));
                }
 
-               PyList_Append(py_domain_list, py_dict);
+               res = PyList_Append(py_domain_list, py_dict);
+               Py_CLEAR(py_dict);
+               if (res == -1) {
+                       Py_CLEAR(py_dict);
+                       talloc_free(frame);
+                       return NULL;
+               }
        }
 
        talloc_free(frame);
@@ -3339,7 +3396,7 @@ static PyObject *py_pdb_enum_trusted_domains(PyObject *self, PyObject *args)
        }
 
        for (i=0; i<num_domains; i++) {
-
+               int res = 0;
                py_domain_info = PyDict_New();
                if (py_domain_info == NULL) {
                        PyErr_NoMemory();
@@ -3371,7 +3428,13 @@ static PyObject *py_pdb_enum_trusted_domains(PyObject *self, PyObject *args)
                PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
                                PyBytes_FromStringAndSize((const char *)td->trust_forest_trust_info.data,
                                                        td->trust_forest_trust_info.length));
-               PyList_Append(py_td_info, py_domain_info);
+               res = PyList_Append(py_td_info, py_domain_info);
+               Py_CLEAR(py_domain_info);
+               if (res == -1) {
+                       Py_CLEAR(py_domain_info);
+                       talloc_free(frame);
+                       return NULL;
+               }
        }
 
        talloc_free(frame);
@@ -3750,7 +3813,17 @@ static PyObject *py_passdb_backends(PyObject *self, PyObject *unused)
        }
 
        while(entry) {
-               PyList_Append(py_blist, PyStr_FromString(entry->name));
+               int res = 0;
+               PyObject *entry_name = PyStr_FromString(entry->name);
+               if (entry_name) {
+                       res = PyList_Append(py_blist, entry_name);
+               }
+               Py_CLEAR(entry_name);
+               if (res == -1) {
+                       Py_CLEAR(py_blist);
+                       talloc_free(frame);
+                       return NULL;
+               }
                entry = entry->next;
        }
 
index dd44c0fcf16115c23f71986ceed7e05f5f4549ee..0e8c89fd58b5a7db87315e50031e2a2c0ef7e8e7 100644 (file)
@@ -52,6 +52,7 @@ static PyObject *py_get_gpo_flags(PyObject *self, PyObject *args)
 
        py_ret = PyList_New(0);
        for (i = 0; ret[i]; i++) {
+               int res = 0;
                PyObject *item = PyStr_FromString(ret[i]);
                if (item == NULL) {
                        talloc_free(mem_ctx);
@@ -59,7 +60,13 @@ static PyObject *py_get_gpo_flags(PyObject *self, PyObject *args)
                        PyErr_NoMemory();
                        return NULL;
                }
-               PyList_Append(py_ret, item);
+               res = PyList_Append(py_ret, item);
+               Py_CLEAR(item);
+               if (res == -1) {
+                       Py_DECREF(py_ret);
+                       talloc_free(mem_ctx);
+                       return NULL;
+               }
        }
 
        talloc_free(mem_ctx);
@@ -94,6 +101,7 @@ static PyObject *py_get_gplink_options(PyObject *self, PyObject *args)
 
        py_ret = PyList_New(0);
        for (i = 0; ret[i]; i++) {
+               int res = 0;
                PyObject *item = PyStr_FromString(ret[i]);
                if (item == NULL) {
                        talloc_free(mem_ctx);
@@ -101,7 +109,13 @@ static PyObject *py_get_gplink_options(PyObject *self, PyObject *args)
                        PyErr_NoMemory();
                        return NULL;
                }
-               PyList_Append(py_ret, item);
+               res = PyList_Append(py_ret, item);
+               Py_CLEAR(item);
+               if (res == -1) {
+                       Py_DECREF(py_ret);
+                       talloc_free(mem_ctx);
+                       return NULL;
+               }
        }
 
        talloc_free(mem_ctx);