pyldb: Raise proper exception when attempting to assign a string to a dn
authorJelmer Vernooij <jelmer@samba.org>
Mon, 3 Aug 2009 16:15:16 +0000 (18:15 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Mon, 3 Aug 2009 16:15:16 +0000 (18:15 +0200)
attribute.

source4/lib/ldb/pyldb.c
source4/lib/ldb/tests/python/api.py

index bcca70eb82bb81e997b72f9d7e5655c2fc4e4243..c7b9b458cdf7f2e81c051a25f76c66dcb5ba0a2e 100644 (file)
@@ -1747,6 +1747,7 @@ static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kw
 {
        const char * const kwnames[] = { "dn", NULL };
        struct ldb_message *ret;
+       TALLOC_CTX *mem_ctx;
        PyObject *pydn = NULL;
        PyLdbMessageObject *py_ret;
 
@@ -1755,8 +1756,15 @@ static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kw
                                         &pydn))
                return NULL;
 
-       ret = ldb_msg_new(NULL);
+       mem_ctx = talloc_new(NULL);
+       if (mem_ctx == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
+
+       ret = ldb_msg_new(mem_ctx);
        if (ret == NULL) {
+               talloc_free(mem_ctx);
                PyErr_NoMemory();
                return NULL;
        }
@@ -1764,7 +1772,7 @@ static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kw
        if (pydn != NULL) {
                struct ldb_dn *dn;
                if (!PyObject_AsDn(NULL, pydn, NULL, &dn)) {
-                       talloc_free(ret);
+                       talloc_free(mem_ctx);
                        return NULL;
                }
                ret->dn = talloc_reference(ret, dn);
@@ -1773,12 +1781,12 @@ static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kw
        py_ret = (PyLdbMessageObject *)type->tp_alloc(type, 0);
        if (py_ret == NULL) {
                PyErr_NoMemory();
-               talloc_free(ret);
+               talloc_free(mem_ctx);
                return NULL;
        }
 
-       py_ret->mem_ctx = talloc_new(NULL);
-       py_ret->msg = talloc_steal(py_ret->mem_ctx, ret);
+       py_ret->mem_ctx = mem_ctx;
+       py_ret->msg = ret;
        return (PyObject *)py_ret;
 }
 
@@ -1805,6 +1813,11 @@ static PyObject *py_ldb_msg_get_dn(PyLdbMessageObject *self, void *closure)
 static int py_ldb_msg_set_dn(PyLdbMessageObject *self, PyObject *value, void *closure)
 {
        struct ldb_message *msg = PyLdbMessage_AsMessage(self);
+       if (!PyLdbDn_Check(value)) {
+               PyErr_SetNone(PyExc_TypeError);
+               return -1;
+       }
+
        msg->dn = talloc_reference(msg, PyLdbDn_AsDn(value));
        return 0;
 }
index 05e3da782edc42b1e8d53dabb71f58d317e54cd5..110b1cabfe764f8d7ee451dc060c1d5023faed7b 100755 (executable)
@@ -279,6 +279,12 @@ class DnTests(unittest.TestCase):
     def setUp(self):
         self.ldb = ldb.Ldb(filename())
 
+    def test_set_dn_invalid(self):
+        x = ldb.Message()
+        def assign():
+            x.dn = "astring"
+        self.assertRaises(TypeError, assign)
+
     def test_eq(self):
         x = ldb.Dn(self.ldb, "dc=foo11,bar=bloe")
         y = ldb.Dn(self.ldb, "dc=foo11,bar=bloe")