pyldb: Fix memory leak in Dn.concat.
[ira/wip.git] / source4 / lib / ldb / pyldb.c
index bceda05e4fd4f7aa8077c551ba83b40f7e8df956..0f666a35f339967fc91b599f409dcc7a3cec6db7 100644 (file)
@@ -1,7 +1,7 @@
 /*
    Unix SMB/CIFS implementation.
 
-   Swig interface to ldb.
+   Python interface to ldb.
 
    Copyright (C) 2005,2006 Tim Potter <tpot@samba.org>
    Copyright (C) 2006 Simo Sorce <idra@samba.org>
@@ -10,7 +10,7 @@
         ** NOTE! The following LGPL license applies to the ldb
         ** library. This does NOT imply that all of Samba is released
         ** under the LGPL
-   
+
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
@@ -41,35 +41,29 @@ typedef intargfunc ssizeargfunc;
 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
 #endif
 
-/* Picked out of thin air. To do this properly, we should probably have some part of the 
- * errors in LDB be allocated to bindings ? */
-#define LDB_ERR_PYTHON_EXCEPTION       142
-
 static PyObject *PyExc_LdbError;
 
-void PyErr_SetLdbError(int ret, struct ldb_context *ldb_ctx)
-{
-       if (ret == LDB_ERR_PYTHON_EXCEPTION)
-               return; /* Python exception should already be set, just keep that */
-       PyErr_SetObject(PyExc_LdbError, Py_BuildValue(discard_const_p(char, "(i,s)"),
-                       ret, ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
-}
+PyAPI_DATA(PyTypeObject) PyLdbMessage;
+PyAPI_DATA(PyTypeObject) PyLdbModule;
+PyAPI_DATA(PyTypeObject) PyLdbDn;
+PyAPI_DATA(PyTypeObject) PyLdb;
+PyAPI_DATA(PyTypeObject) PyLdbMessageElement;
+PyAPI_DATA(PyTypeObject) PyLdbTree;
 
 static PyObject *PyObject_FromLdbValue(struct ldb_context *ldb_ctx, 
                                                           struct ldb_message_element *el, 
                                                           struct ldb_val *val)
 {
-       const struct ldb_schema_attribute *a;
        struct ldb_val new_val;
        TALLOC_CTX *mem_ctx = talloc_new(NULL);
        PyObject *ret;
-       
+
        new_val = *val;
 
        ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
-       
+
        talloc_free(mem_ctx);
-       
+
        return ret;
 }
 
@@ -135,7 +129,7 @@ static struct ldb_result *PyLdbResult_AsResult(TALLOC_CTX *mem_ctx,
 {
        struct ldb_result *res;
        int i;
-       
+
        if (obj == Py_None)
                return NULL;
 
@@ -212,7 +206,11 @@ static int py_ldb_dn_compare(PyLdbDnObject *dn1, PyLdbDnObject *dn2)
 static PyObject *py_ldb_dn_get_parent(PyLdbDnObject *self)
 {
        struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self);
-       return PyLdbDn_FromDn(ldb_dn_get_parent(NULL, dn));
+       struct ldb_dn *parent;
+
+       parent = ldb_dn_get_parent(NULL, dn);
+
+       return PyLdbDn_FromDn(parent);
 }
 
 #define dn_ldb_ctx(dn) ((struct ldb_context *)dn)
@@ -293,11 +291,20 @@ static PyObject *py_ldb_dn_concat(PyLdbDnObject *self, PyObject *py_other)
 {
        struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self), 
                                  *other;
-       struct ldb_dn *ret = ldb_dn_copy(NULL, dn);
+       PyLdbDnObject *py_ret;
+       
        if (!PyObject_AsDn(NULL, py_other, NULL, &other))
                return NULL;
-       ldb_dn_add_child(ret, other);
-       return PyLdbDn_FromDn(ret);
+
+       py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
+       if (py_ret == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
+       py_ret->mem_ctx = talloc_new(NULL);
+       py_ret->dn = ldb_dn_copy(py_ret->mem_ctx, dn);
+       ldb_dn_add_child(py_ret->dn, other);
+       return (PyObject *)py_ret;
 }
 
 static PySequenceMethods py_ldb_dn_seq = {
@@ -311,6 +318,7 @@ static PyObject *py_ldb_dn_new(PyTypeObject *type, PyObject *args, PyObject *kwa
        char *str;
        PyObject *py_ldb;
        struct ldb_context *ldb_ctx;
+       TALLOC_CTX *mem_ctx;
        PyLdbDnObject *py_ret;
        const char * const kwnames[] = { "ldb", "dn", NULL };
 
@@ -320,22 +328,28 @@ static PyObject *py_ldb_dn_new(PyTypeObject *type, PyObject *args, PyObject *kwa
                return NULL;
 
        ldb_ctx = PyLdb_AsLdbContext(py_ldb);
-       
-       ret = ldb_dn_new(ldb_ctx, ldb_ctx, str);
-       /* ldb_dn_new() doesn't accept NULL as memory context, so 
-          we do it this way... */
-       talloc_steal(NULL, ret);
+
+       mem_ctx = talloc_new(NULL);
+       if (mem_ctx == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
+
+       ret = ldb_dn_new(mem_ctx, ldb_ctx, str);
 
        if (ret == NULL || !ldb_dn_validate(ret)) {
+               talloc_free(mem_ctx);
                PyErr_SetString(PyExc_ValueError, "unable to parse dn string");
                return NULL;
        }
 
        py_ret = (PyLdbDnObject *)type->tp_alloc(type, 0);
        if (ret == NULL) {
+               talloc_free(mem_ctx);
                PyErr_NoMemory();
                return NULL;
        }
+       py_ret->mem_ctx = mem_ctx;
        py_ret->dn = ret;
        return (PyObject *)py_ret;
 }
@@ -343,6 +357,11 @@ static PyObject *py_ldb_dn_new(PyTypeObject *type, PyObject *args, PyObject *kwa
 PyObject *PyLdbDn_FromDn(struct ldb_dn *dn)
 {
        PyLdbDnObject *py_ret;
+
+       if (dn == NULL) {
+               Py_RETURN_NONE;
+       }
+
        py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
        if (py_ret == NULL) {
                PyErr_NoMemory();
@@ -384,14 +403,14 @@ static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *
 static PyObject *py_ldb_set_debug(PyLdbObject *self, PyObject *args)
 {
        PyObject *cb;
-       
+
        if (!PyArg_ParseTuple(args, "O", &cb))
                return NULL;
 
        Py_INCREF(cb);
        /* FIXME: Where do we DECREF cb ? */
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_set_debug(self->ldb_ctx, py_ldb_debug, cb), PyLdb_AsLdbContext(self));
-       
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_set_debug(self->ldb_ctx, py_ldb_debug, cb), PyLdb_AsLdbContext(self));
+
        Py_RETURN_NONE;
 }
 
@@ -419,25 +438,25 @@ static PyObject *py_ldb_set_modules_dir(PyTypeObject *self, PyObject *args)
 
 static PyObject *py_ldb_transaction_start(PyLdbObject *self)
 {
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_start(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_start(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
        Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_transaction_commit(PyLdbObject *self)
 {
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
        Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_transaction_cancel(PyLdbObject *self)
 {
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_cancel(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_cancel(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
        Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_setup_wellknown_attributes(PyLdbObject *self)
 {
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
        Py_RETURN_NONE;
 }
 
@@ -525,11 +544,11 @@ static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs)
                if (options == NULL)
                        return -1;
        }
-       
+
        if (url != NULL) {
                ret = ldb_connect(ldb, url, flags, options);
                if (ret != LDB_SUCCESS) {
-                       PyErr_SetLdbError(ret, ldb);
+                       PyErr_SetLdbError(PyExc_LdbError, ret, ldb);
                        return -1;
                }
        }
@@ -578,11 +597,11 @@ static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwa
                if (options == NULL)
                        return NULL;
        }
-       
+
        ret = ldb_connect(PyLdb_AsLdbContext(self), url, flags, options);
        talloc_free(options);
 
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
 
        Py_RETURN_NONE;
 }
@@ -600,7 +619,7 @@ static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
        }
 
        ret = ldb_modify(PyLdb_AsLdbContext(self), PyLdbMessage_AsMessage(py_msg));
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
 
        Py_RETURN_NONE;
 }
@@ -655,9 +674,9 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
        } else {
                msg = PyLdbMessage_AsMessage(py_msg);
        }
-       
+
        ret = ldb_add(PyLdb_AsLdbContext(self), msg);
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
 
        Py_RETURN_NONE;
 }
@@ -677,7 +696,7 @@ static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args)
                return NULL;
 
        ret = ldb_delete(ldb, dn);
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
 
        Py_RETURN_NONE;
 }
@@ -699,7 +718,7 @@ static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
                return NULL;
 
        ret = ldb_rename(ldb, dn1, dn2);
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
 
        Py_RETURN_NONE;
 }
@@ -725,7 +744,7 @@ static PyObject *py_ldb_schema_attribute_add(PyLdbObject *self, PyObject *args)
 
        ret = ldb_schema_attribute_add(PyLdb_AsLdbContext(self), attribute, flags, syntax);
 
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
 
        Py_RETURN_NONE;
 }
@@ -772,18 +791,18 @@ static PyObject *py_ldb_schema_format_value(PyLdbObject *self, PyObject *args)
 
        if (!PyArg_ParseTuple(args, "sO", &element_name, &val))
                return NULL;
-       
+
        mem_ctx = talloc_new(NULL);
-       
+
        old_val.data = (uint8_t *)PyString_AsString(val);
        old_val.length = PyString_Size(val);
-               
+
        a = ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self), element_name);
 
        if (a == NULL) {
                Py_RETURN_NONE;
        }
-       
+
        if (a->syntax->ldif_write_fn(PyLdb_AsLdbContext(self), mem_ctx, &old_val, &new_val) != 0) {
                talloc_free(mem_ctx);
                Py_RETURN_NONE;
@@ -811,6 +830,7 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
        struct ldb_context *ldb_ctx;
        struct ldb_control **parsed_controls;
        struct ldb_dn *base;
+       PyObject *py_ret;
 
        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OizOO",
                                         discard_const_p(char *, kwnames),
@@ -822,7 +842,7 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
        if (py_attrs == Py_None) {
                attrs = NULL;
        } else {
-               attrs = PyList_AsStringList(ldb_ctx, py_attrs, "attrs");
+               attrs = PyList_AsStringList(NULL, py_attrs, "attrs");
                if (attrs == NULL)
                        return NULL;
        }
@@ -830,8 +850,10 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
        if (py_base == Py_None) {
                base = ldb_get_default_basedn(ldb_ctx);
        } else {
-               if (!PyObject_AsDn(ldb_ctx, py_base, ldb_ctx, &base))
+               if (!PyObject_AsDn(ldb_ctx, py_base, ldb_ctx, &base)) {
+                       talloc_free(attrs);
                        return NULL;
+               }
        }
 
        if (py_controls == Py_None) {
@@ -845,6 +867,7 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
        res = talloc_zero(ldb_ctx, struct ldb_result);
        if (res == NULL) {
                PyErr_NoMemory();
+               talloc_free(attrs);
                return NULL;
        }
 
@@ -858,14 +881,16 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
                                   ldb_search_default_callback,
                                   NULL);
 
+       talloc_steal(req, attrs);
+
        if (ret != LDB_SUCCESS) {
                talloc_free(res);
-               PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb_ctx);
+               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
                return NULL;
        }
 
        ret = ldb_request(ldb_ctx, req);
-               
+
        if (ret == LDB_SUCCESS) {
                ret = ldb_wait(req->handle, LDB_WAIT_ALL);
        }
@@ -874,11 +899,15 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
 
        if (ret != LDB_SUCCESS) {
                talloc_free(res);
-               PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb_ctx);
+               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
                return NULL;
        }
 
-       return PyLdbResult_FromResult(res);
+       py_ret = PyLdbResult_FromResult(res);
+
+       talloc_free(res);
+
+       return py_ret;
 }
 
 static PyObject *py_ldb_get_opaque(PyLdbObject *self, PyObject *args)
@@ -1044,7 +1073,7 @@ static int py_ldb_contains(PyLdbObject *self, PyObject *obj)
 
        ret = ldb_search(ldb_ctx, ldb_ctx, &result, dn, LDB_SCOPE_BASE, NULL, NULL);
        if (ret != LDB_SUCCESS) {
-               PyErr_SetLdbError(ret, ldb_ctx);
+               PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
                return -1;
        }
 
@@ -1124,11 +1153,12 @@ static PyObject *py_ldb_module_del_transaction(PyLdbModuleObject *self)
 
 static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, PyObject *kwargs)
 {
-       PyObject *py_base, *py_tree, *py_attrs;
+       PyObject *py_base, *py_tree, *py_attrs, *py_ret;
        int ret, scope;
        struct ldb_request *req;
        const char * const kwnames[] = { "base", "scope", "tree", "attrs", NULL };
        struct ldb_module *mod;
+       const char * const*attrs;
 
        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OiOO",
                                         discard_const_p(char *, kwnames),
@@ -1137,17 +1167,33 @@ static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, P
 
        mod = self->mod;
 
+       if (py_attrs == Py_None) {
+               attrs = NULL;
+       } else {
+               attrs = PyList_AsStringList(NULL, py_attrs, "attrs");
+               if (attrs == NULL)
+                       return NULL;
+       }
+
        ret = ldb_build_search_req(&req, mod->ldb, NULL, PyLdbDn_AsDn(py_base), 
-                            scope, NULL /* expr */, py_attrs == Py_None?NULL:PyList_AsStringList(req, py_attrs, "attrs"),
+                            scope, NULL /* expr */, attrs,
                             NULL /* controls */, NULL, NULL, NULL);
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
+
+       talloc_steal(req, attrs);
+
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
+
+       req->op.search.res = NULL;
 
        ret = mod->ops->search(mod, req);
-       talloc_free(req);
 
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
+
+       py_ret = PyLdbResult_FromResult(req->op.search.res);
 
-       return PyLdbResult_FromResult(req->op.search.res);
+       talloc_free(req);
+
+       return py_ret;  
 }
 
 
@@ -1168,7 +1214,7 @@ static PyObject *py_ldb_module_add(PyLdbModuleObject *self, PyObject *args)
        mod = PyLdbModule_AsModule(self);
        ret = mod->ops->add(mod, req);
 
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
 
        Py_RETURN_NONE;
 }
@@ -1182,15 +1228,15 @@ static PyObject *py_ldb_module_modify(PyLdbModuleObject *self, PyObject *args)
 
        if (!PyArg_ParseTuple(args, "O", &py_message))
                return NULL;
-       
+
        req = talloc_zero(NULL, struct ldb_request);
        req->operation = LDB_MODIFY;
        req->op.mod.message = PyLdbMessage_AsMessage(py_message);
-       
+
        mod = PyLdbModule_AsModule(self);
        ret = mod->ops->modify(mod, req);
 
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
 
        Py_RETURN_NONE;
 }
@@ -1203,14 +1249,14 @@ static PyObject *py_ldb_module_delete(PyLdbModuleObject *self, PyObject *args)
 
        if (!PyArg_ParseTuple(args, "O", &py_dn))
                return NULL;
-       
+
        req = talloc_zero(NULL, struct ldb_request);
        req->operation = LDB_DELETE;
        req->op.del.dn = PyLdbDn_AsDn(py_dn);
-       
+
        ret = PyLdbModule_AsModule(self)->ops->del(PyLdbModule_AsModule(self), req);
 
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
 
        Py_RETURN_NONE;
 }
@@ -1223,16 +1269,16 @@ static PyObject *py_ldb_module_rename(PyLdbModuleObject *self, PyObject *args)
 
        if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
                return NULL;
-       
+
        req = talloc_zero(NULL, struct ldb_request);
 
        req->operation = LDB_RENAME;
        req->op.rename.olddn = PyLdbDn_AsDn(py_dn1);
        req->op.rename.newdn = PyLdbDn_AsDn(py_dn2);
-       
+
        ret = PyLdbModule_AsModule(self)->ops->rename(PyLdbModule_AsModule(self), req);
 
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
 
        Py_RETURN_NONE;
 }
@@ -1648,18 +1694,24 @@ static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kw
                return NULL;
        }
 
-       if (pydn != NULL)
-               if (!PyObject_AsDn(NULL, pydn, NULL, &ret->dn))
+       if (pydn != NULL) {
+               struct ldb_dn *dn;
+               if (!PyObject_AsDn(NULL, pydn, NULL, &dn)) {
+                       talloc_free(ret);
                        return NULL;
+               }
+               ret->dn = talloc_reference(ret, dn);
+       }
 
        py_ret = (PyLdbMessageObject *)type->tp_alloc(type, 0);
        if (py_ret == NULL) {
                PyErr_NoMemory();
+               talloc_free(ret);
                return NULL;
        }
 
        py_ret->mem_ctx = talloc_new(NULL);
-       py_ret->msg = talloc_reference(py_ret->mem_ctx, ret);
+       py_ret->msg = talloc_steal(py_ret->mem_ctx, ret);
        return (PyObject *)py_ret;
 }
 
@@ -1679,12 +1731,14 @@ PyObject *PyLdbMessage_FromMessage(struct ldb_message *msg)
 
 static PyObject *py_ldb_msg_get_dn(PyLdbMessageObject *self, void *closure)
 {
-       return PyLdbDn_FromDn(PyLdbMessage_AsMessage(self)->dn);
+       struct ldb_message *msg = PyLdbMessage_AsMessage(self);
+       return PyLdbDn_FromDn(msg->dn);
 }
 
 static int py_ldb_msg_set_dn(PyLdbMessageObject *self, PyObject *value, void *closure)
 {
-       PyLdbMessage_AsMessage(self)->dn = PyLdbDn_AsDn(value);
+       struct ldb_message *msg = PyLdbMessage_AsMessage(self);
+       msg->dn = talloc_reference(msg, PyLdbDn_AsDn(value));
        return 0;
 }
 
@@ -1731,7 +1785,7 @@ PyObject *PyLdbTree_FromTree(struct ldb_parse_tree *tree)
                PyErr_NoMemory();
                return NULL;
        }
-       
+
        ret->mem_ctx = talloc_new(NULL);
        ret->tree = talloc_reference(ret->mem_ctx, tree);
        return (PyObject *)ret;
@@ -2045,7 +2099,7 @@ static PyObject *py_register_module(PyObject *module, PyObject *args)
 
        ret = ldb_register_module(ops);
 
-       PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
 
        Py_RETURN_NONE;
 }