r26631: ldb/python: Fix missing elements bug and memory leak. * Don't increase the...
authorJelmer Vernooij <jelmer@samba.org>
Sun, 30 Dec 2007 22:46:14 +0000 (16:46 -0600)
committerStefan Metzmacher <metze@samba.org>
Sun, 30 Dec 2007 16:51:07 +0000 (10:51 -0600)
source/lib/ldb/ldb.i
source/lib/ldb/ldb_wrap.c

index 7960d38bb503cc038be3afac81e7c4b3dee032c0..5569d08ebbc2ed28073db21d32ef4f557a342b9c 100644 (file)
@@ -208,10 +208,11 @@ int ldb_dn_from_pyobject(TALLOC_CTX *mem_ctx, PyObject *object,
     return ret;
 }
 
-ldb_msg_element *ldb_msg_element_from_pyobject(PyObject *set_obj, int flags,
+ldb_msg_element *ldb_msg_element_from_pyobject(TALLOC_CTX *mem_ctx,
+                                               PyObject *set_obj, int flags,
                                                const char *attr_name)
 {
-    struct ldb_message_element *me = talloc(NULL, struct ldb_message_element);
+    struct ldb_message_element *me = talloc(mem_ctx, struct ldb_message_element);
     me->name = attr_name;
     me->flags = flags;
     if (PyString_Check(set_obj)) {
@@ -275,7 +276,7 @@ typedef struct ldb_message_element {
 
         ldb_msg_element(PyObject *set_obj, int flags=0, const char *name = NULL)
         {
-            return ldb_msg_element_from_pyobject(set_obj, flags, name);
+            return ldb_msg_element_from_pyobject(NULL, set_obj, flags, name);
         }
 #endif
         ~ldb_msg_element() { talloc_free($self); }
@@ -350,7 +351,7 @@ typedef struct ldb_message {
 
         void __setitem__(const char *attr_name, PyObject *val)
         {
-            struct ldb_message_element *el = ldb_msg_element_from_pyobject(
+            struct ldb_message_element *el = ldb_msg_element_from_pyobject(NULL,
                                                 val, 0, attr_name);
             talloc_steal($self, el);
             ldb_msg_remove_attr($self, attr_name);
@@ -486,8 +487,7 @@ typedef struct ldb_context {
             ldb_msg *msg = NULL;
             if (PyDict_Check(py_msg)) {
                 msg = ldb_msg_new(NULL);
-                msg->num_elements = PyDict_Size(py_msg) - 1; /* dn isn't in there */
-                msg->elements = talloc_zero_array(msg, struct ldb_message_element, msg->num_elements+1);
+                msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_msg));
                 msg_pos = dict_pos = 0;
                 while (PyDict_Next(py_msg, &dict_pos, &key, &value)) {
                     if (!strcmp(PyString_AsString(key), "dn")) {
@@ -495,17 +495,22 @@ typedef struct ldb_context {
                             return LDB_ERR_OTHER;
                         }
                     } else {
-                        msgel = ldb_msg_element_from_pyobject(value, 0, PyString_AsString(key));
+                        msgel = ldb_msg_element_from_pyobject(msg->elements, value, 0, PyString_AsString(key));
+                        if (msgel == NULL) {
+                            SWIG_exception(SWIG_TypeError, "unable to import element");
+                            return LDB_ERR_OTHER;
+                        }
                         memcpy(&msg->elements[msg_pos], msgel, sizeof(*msgel));
                         msg_pos++;
                     }
-                    dict_pos++;
                 }
 
                 if (msg->dn == NULL) {
                     SWIG_exception(SWIG_TypeError, "no dn set");
                     return LDB_ERR_OTHER;
                 }
+
+                msg->num_elements = msg_pos;
             } else {
                 if (SWIG_ConvertPtr(py_msg, (void **)&msg, SWIGTYPE_p_ldb_message, 0) != 0)
                     return LDB_ERR_OTHER;
index d29c0289f60fb14d984a6ce5bc7af6c8e09b4d16..5f2f73e7495f95a697ade1d6cb643b154af5f792 100644 (file)
@@ -2687,10 +2687,11 @@ int ldb_dn_from_pyobject(TALLOC_CTX *mem_ctx, PyObject *object,
     return ret;
 }
 
-ldb_msg_element *ldb_msg_element_from_pyobject(PyObject *set_obj, int flags,
+ldb_msg_element *ldb_msg_element_from_pyobject(TALLOC_CTX *mem_ctx,
+                                               PyObject *set_obj, int flags,
                                                const char *attr_name)
 {
-    struct ldb_message_element *me = talloc(NULL, struct ldb_message_element);
+    struct ldb_message_element *me = talloc(mem_ctx, struct ldb_message_element);
     me->name = attr_name;
     me->flags = flags;
     if (PyString_Check(set_obj)) {
@@ -2886,7 +2887,7 @@ SWIG_AsVal_int (PyObject * obj, int *val)
 }
 
 SWIGINTERN ldb_msg_element *new_ldb_msg_element(PyObject *set_obj,int flags,char const *name){
-            return ldb_msg_element_from_pyobject(set_obj, flags, name);
+            return ldb_msg_element_from_pyobject(NULL, set_obj, flags, name);
         }
 SWIGINTERN void delete_ldb_msg_element(ldb_msg_element *self){ talloc_free(self); }
 
@@ -2918,7 +2919,7 @@ SWIGINTERN void ldb_msg___setitem____SWIG_0(ldb_msg *self,char const *attr_name,
             ldb_msg_add(self, el, val->flags);
         }
 SWIGINTERN void ldb_msg___setitem____SWIG_1(ldb_msg *self,char const *attr_name,PyObject *val){
-            struct ldb_message_element *el = ldb_msg_element_from_pyobject(
+            struct ldb_message_element *el = ldb_msg_element_from_pyobject(NULL,
                                                 val, 0, attr_name);
             talloc_steal(self, el);
             ldb_msg_remove_attr(self, attr_name);
@@ -3041,8 +3042,7 @@ SWIGINTERN ldb_error ldb_add__SWIG_1(ldb *self,PyObject *py_msg){
             ldb_msg *msg = NULL;
             if (PyDict_Check(py_msg)) {
                 msg = ldb_msg_new(NULL);
-                msg->num_elements = PyDict_Size(py_msg) - 1; /* dn isn't in there */
-                msg->elements = talloc_zero_array(msg, struct ldb_message_element, msg->num_elements+1);
+                msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_msg));
                 msg_pos = dict_pos = 0;
                 while (PyDict_Next(py_msg, &dict_pos, &key, &value)) {
                     if (!strcmp(PyString_AsString(key), "dn")) {
@@ -3050,17 +3050,22 @@ SWIGINTERN ldb_error ldb_add__SWIG_1(ldb *self,PyObject *py_msg){
                             return 80;
                         }
                     } else {
-                        msgel = ldb_msg_element_from_pyobject(value, 0, PyString_AsString(key));
+                        msgel = ldb_msg_element_from_pyobject(msg->elements, value, 0, PyString_AsString(key));
+                        if (msgel == NULL) {
+                            SWIG_exception(SWIG_TypeError, "unable to import element");
+                            return 80;
+                        }
                         memcpy(&msg->elements[msg_pos], msgel, sizeof(*msgel));
                         msg_pos++;
                     }
-                    dict_pos++;
                 }
 
                 if (msg->dn == NULL) {
                     SWIG_exception(SWIG_TypeError, "no dn set");
                     return 80;
                 }
+
+                msg->num_elements = msg_pos;
             } else {
                 if (SWIG_ConvertPtr(py_msg, (void **)&msg, SWIGTYPE_p_ldb_message, 0) != 0)
                     return 80;