From f4b30bebca41a2cf41d94acd0bd333358aebf8d0 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Sun, 30 Dec 2007 16:46:14 -0600 Subject: r26631: ldb/python: Fix missing elements bug and memory leak. * Don't increase the PyDict_Next() counter - Python already does that for us. * Fix a talloc(NULL, ...) memory leak in the code that constructed ldb message elements. (This used to be commit dc2a612927289da78017abf4ad51a4d51292e3a1) --- source4/lib/ldb/ldb.i | 21 +++++++++++++-------- source4/lib/ldb/ldb_wrap.c | 21 +++++++++++++-------- 2 files changed, 26 insertions(+), 16 deletions(-) (limited to 'source4') diff --git a/source4/lib/ldb/ldb.i b/source4/lib/ldb/ldb.i index 7960d38bb5..5569d08ebb 100644 --- a/source4/lib/ldb/ldb.i +++ b/source4/lib/ldb/ldb.i @@ -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; diff --git a/source4/lib/ldb/ldb_wrap.c b/source4/lib/ldb/ldb_wrap.c index d29c0289f6..5f2f73e749 100644 --- a/source4/lib/ldb/ldb_wrap.c +++ b/source4/lib/ldb/ldb_wrap.c @@ -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; -- cgit