summaryrefslogtreecommitdiff
path: root/source4/lib/ldb/common
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2005-01-02 07:49:29 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 13:07:55 -0500
commit1a988ec9af7960616fb4661b20d86ff05146d836 (patch)
tree725eb92b6b97133b23972215f5c69117e29c339c /source4/lib/ldb/common
parent65f96eba32b93ced0717c2639007bba59da55fa4 (diff)
downloadsamba-1a988ec9af7960616fb4661b20d86ff05146d836.tar.gz
samba-1a988ec9af7960616fb4661b20d86ff05146d836.tar.bz2
samba-1a988ec9af7960616fb4661b20d86ff05146d836.zip
r4474: - converted ldb to use talloc internally
- added gcov flags to Makefile.ldb - expanded ldb test suite to get more coverage (This used to be commit 0ab98f50a7e0fe15347a99e5c29a6590a87729a0)
Diffstat (limited to 'source4/lib/ldb/common')
-rw-r--r--source4/lib/ldb/common/ldb_alloc.c176
-rw-r--r--source4/lib/ldb/common/ldb_ldif.c52
-rw-r--r--source4/lib/ldb/common/ldb_modules.c33
-rw-r--r--source4/lib/ldb/common/ldb_msg.c54
-rw-r--r--source4/lib/ldb/common/ldb_parse.c119
-rw-r--r--source4/lib/ldb/common/ldb_utf8.c2
6 files changed, 110 insertions, 326 deletions
diff --git a/source4/lib/ldb/common/ldb_alloc.c b/source4/lib/ldb/common/ldb_alloc.c
deleted file mode 100644
index 7dc12b142e..0000000000
--- a/source4/lib/ldb/common/ldb_alloc.c
+++ /dev/null
@@ -1,176 +0,0 @@
-/*
- ldb database library
-
- Copyright (C) Andrew Tridgell 2004
-
- ** 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
- version 2 of the License, or (at your option) any later version.
-
- This library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public
- License along with this library; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-*/
-
-/*
- * Name: ldb
- *
- * Component: ldb alloc
- *
- * Description: functions for memory allocation
- *
- * Author: Andrew Tridgell
- */
-
-#include "includes.h"
-#include "ldb/include/ldb.h"
-#include "ldb/include/ldb_private.h"
-
-
-/*
- this allows the user to choose their own allocation function
-*/
-int ldb_set_alloc(struct ldb_context *ldb,
- void *(*alloc)(const void *context, void *ptr, size_t size),
- void *context)
-{
- ldb->alloc_ops.alloc = alloc;
- ldb->alloc_ops.context = context;
- return 0;
-}
-
-/*
- this is the default memory allocation function
-*/
-static void *ldb_default_alloc(const void *context, void *ptr, size_t size)
-{
- /* by setting LDB_ALLOC_OFS to non-zero the test suite can
- catch any places where we incorrectly use the libc alloc
- funcitons directly */
-#define LDB_ALLOC_OFS 4
- /* we don't assume a modern realloc function */
- if (ptr == NULL) {
- ptr = malloc(size+LDB_ALLOC_OFS);
- if (ptr) return ((char *)ptr)+LDB_ALLOC_OFS;
- return NULL;
- }
- if (size == 0) {
- free(((char *)ptr)-LDB_ALLOC_OFS);
- return NULL;
- }
- ptr = realloc(((char *)ptr)-LDB_ALLOC_OFS, size+LDB_ALLOC_OFS);
- if (ptr) {
- return ((char *)ptr)+LDB_ALLOC_OFS;
- }
- return NULL;
-}
-
-/*
- all memory allocation goes via this function
-*/
-void *ldb_realloc(struct ldb_context *ldb, void *ptr, size_t size)
-{
- if (!ldb->alloc_ops.alloc) {
- ldb_set_alloc(ldb, ldb_default_alloc, NULL);
- }
- return ldb->alloc_ops.alloc(ldb->alloc_ops.context, ptr, size);
-}
-
-void *ldb_malloc(struct ldb_context *ldb, size_t size)
-{
- return ldb_realloc(ldb, NULL, size);
-}
-
-void ldb_free(struct ldb_context *ldb, void *ptr)
-{
- if (ptr != NULL) {
- ldb_realloc(ldb, ptr, 0);
- }
-}
-
-void *ldb_strndup(struct ldb_context *ldb, const char *str, size_t maxlen)
-{
- size_t len = strnlen(str, maxlen);
- void *ret;
- ret = ldb_realloc(ldb, NULL, len+1);
- if (ret) {
- memcpy(ret, str, len);
- ((char *)ret)[len] = 0;
- }
- return ret;
-}
-
-void *ldb_strdup(struct ldb_context *ldb, const char *str)
-{
- size_t len = strlen(str);
- void *ret;
- ret = ldb_realloc(ldb, NULL, len+1);
- if (ret) {
- memcpy(ret, str, len+1);
- }
- return ret;
-}
-
-/*
- a ldb wrapper for asprintf(), using ldb_malloc()
-*/
-int ldb_asprintf(struct ldb_context *ldb, char **strp, const char *fmt, ...)
-{
- int len, len2;
- va_list ap;
-
- *strp = NULL;
-
- va_start(ap, fmt);
- len = vsnprintf(NULL, 0, fmt, ap);
- va_end(ap);
- if (len < 0) {
- return len;
- }
-
- *strp = ldb_malloc(ldb, len+1);
- if (! *strp) {
- return -1;
- }
-
- va_start(ap, fmt);
- len2 = vsnprintf(*strp, len+1, fmt, ap);
- va_end(ap);
-
- if (len2 != len) {
- /* buggy (or non-C99) vsnprintf function */
- ldb_free(ldb, *strp);
- return -1;
- }
-
- return len;
-}
-
-/*
- realloc an array, checking for integer overflow in the array size
-*/
-void *ldb_realloc_array(struct ldb_context *ldb,
- void *ptr, size_t el_size, unsigned count)
-{
-#define MAX_MALLOC_SIZE 0x7fffffff
-
- if (count == 0 ||
- count >= MAX_MALLOC_SIZE/el_size) {
- return NULL;
- }
- if (!ptr) {
- return ldb_malloc(ldb, el_size * count);
- }
- return ldb_realloc(ldb, ptr, el_size * count);
-}
-
diff --git a/source4/lib/ldb/common/ldb_ldif.c b/source4/lib/ldb/common/ldb_ldif.c
index 94e15805a6..d20a2a3553 100644
--- a/source4/lib/ldb/common/ldb_ldif.c
+++ b/source4/lib/ldb/common/ldb_ldif.c
@@ -97,7 +97,7 @@ char *ldb_base64_encode(struct ldb_context *ldb, const char *buf, int len)
int bytes = (len*8 + 5)/6;
char *out;
- out = ldb_malloc(ldb, bytes+2);
+ out = talloc_array_p(ldb, char, bytes+2);
if (!out) return NULL;
for (i=0;i<bytes;i++) {
@@ -185,7 +185,7 @@ static int base64_encode_f(struct ldb_context *ldb,
ret = fold_string(fprintf_fn, private_data, b, strlen(b), start_pos);
- ldb_free(ldb, b);
+ talloc_free(b);
return ret;
}
@@ -212,7 +212,7 @@ int ldb_ldif_write(struct ldb_context *ldb,
int total=0, ret;
const struct ldb_message *msg;
- msg = &ldif->msg;
+ msg = ldif->msg;
ret = fprintf_fn(private_data, "dn: %s\n", msg->dn);
CHECK_RET;
@@ -305,9 +305,9 @@ static char *next_chunk(struct ldb_context *ldb,
if (chunk_size+1 >= alloc_size) {
char *c2;
alloc_size += 1024;
- c2 = ldb_realloc_p(ldb, chunk, char, alloc_size);
+ c2 = talloc_realloc_p(ldb, chunk, char, alloc_size);
if (!c2) {
- ldb_free(ldb, chunk);
+ talloc_free(chunk);
errno = ENOMEM;
return NULL;
}
@@ -416,15 +416,7 @@ static int next_attr(char **s, const char **attr, struct ldb_val *value)
*/
void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif)
{
- struct ldb_message *msg = &ldif->msg;
- unsigned int i;
- for (i=0;i<msg->num_elements;i++) {
- if (msg->elements[i].name) ldb_free(ldb, msg->elements[i].name);
- if (msg->elements[i].values) ldb_free(ldb, msg->elements[i].values);
- }
- if (msg->elements) ldb_free(ldb, msg->elements);
- if (msg->private_data) ldb_free(ldb, msg->private_data);
- ldb_free(ldb, ldif);
+ talloc_free(ldif);
}
/*
@@ -435,8 +427,8 @@ static int msg_add_empty(struct ldb_context *ldb,
{
struct ldb_message_element *el2, *el;
- el2 = ldb_realloc_p(ldb, msg->elements,
- struct ldb_message_element, msg->num_elements+1);
+ el2 = talloc_realloc_p(msg, msg->elements,
+ struct ldb_message_element, msg->num_elements+1);
if (!el2) {
errno = ENOMEM;
return -1;
@@ -446,7 +438,7 @@ static int msg_add_empty(struct ldb_context *ldb,
el = &msg->elements[msg->num_elements];
- el->name = ldb_strdup(ldb, name);
+ el->name = talloc_strdup(msg->elements, name);
el->num_values = 0;
el->values = NULL;
el->flags = flags;
@@ -476,11 +468,17 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
value.data = NULL;
- ldif = ldb_malloc_p(ldb, struct ldb_ldif);
+ ldif = talloc_p(ldb, struct ldb_ldif);
if (!ldif) return NULL;
+ ldif->msg = talloc_p(ldif, struct ldb_message);
+ if (ldif->msg == NULL) {
+ talloc_free(ldif);
+ return NULL;
+ }
+
ldif->changetype = LDB_CHANGETYPE_NONE;
- msg = &ldif->msg;
+ msg = ldif->msg;
msg->dn = NULL;
msg->elements = NULL;
@@ -558,8 +556,8 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
flags == el->flags) {
/* its a continuation */
el->values =
- ldb_realloc_p(ldb, el->values,
- struct ldb_val, el->num_values+1);
+ talloc_realloc_p(msg->elements, el->values,
+ struct ldb_val, el->num_values+1);
if (!el->values) {
goto failed;
}
@@ -567,16 +565,16 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
el->num_values++;
} else {
/* its a new attribute */
- msg->elements = ldb_realloc_p(ldb, msg->elements,
- struct ldb_message_element,
- msg->num_elements+1);
+ msg->elements = talloc_realloc_p(ldif, msg->elements,
+ struct ldb_message_element,
+ msg->num_elements+1);
if (!msg->elements) {
goto failed;
}
el = &msg->elements[msg->num_elements];
el->flags = flags;
- el->name = ldb_strdup(ldb, attr);
- el->values = ldb_malloc_p(ldb, struct ldb_val);
+ el->name = talloc_strdup(msg->elements, attr);
+ el->values = talloc_p(msg->elements, struct ldb_val);
if (!el->values || !el->name) {
goto failed;
}
@@ -589,7 +587,7 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
return ldif;
failed:
- if (ldif) ldb_ldif_read_free(ldb, ldif);
+ talloc_free(ldif);
return NULL;
}
diff --git a/source4/lib/ldb/common/ldb_modules.c b/source4/lib/ldb/common/ldb_modules.c
index 71ec3fdc00..4f53535bc4 100644
--- a/source4/lib/ldb/common/ldb_modules.c
+++ b/source4/lib/ldb/common/ldb_modules.c
@@ -57,21 +57,19 @@ int ldb_load_modules(struct ldb_context *ldb, const char *options[])
pn = 0;
if (options) {
-
for (i = 0; options[i] != NULL; i++) {
-
- if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
-
- p = q = ldb_strdup(ldb, &options[i][LDB_MODULE_PREFIX_LEN]);
+ if (strncmp(options[i], LDB_MODULE_PREFIX,
+ LDB_MODULE_PREFIX_LEN) == 0) {
+ p = q = talloc_strdup(ldb, &options[i][LDB_MODULE_PREFIX_LEN]);
if (*q != ':') {
- ldb_free(ldb, q);
+ talloc_free(q);
return -1;
}
do {
*p = '\0';
q = p + 1;
pn++;
- modules = ldb_realloc_array(ldb, modules, sizeof(char *), pn);
+ modules = talloc_realloc_p(ldb, modules, char *, pn);
if (!modules) {
ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in register_modules()\n");
return -1;
@@ -82,10 +80,12 @@ int ldb_load_modules(struct ldb_context *ldb, const char *options[])
}
}
- if (!modules && strcmp("ldap", ldb->modules->ops->name)) { /* no modules in the options, look for @MODULES in the db (not for ldap) */
+ if (!modules && strcmp("ldap", ldb->modules->ops->name)) {
+ /* no modules in the options, look for @MODULES in the
+ db (not for ldap) */
int ret, j, k;
const char * const attrs[] = { "@MODULE" , NULL};
- struct ldb_message **msg;
+ struct ldb_message **msg = NULL;
ret = ldb_search(ldb, "", LDB_SCOPE_BASE, "dn=@MODULES", attrs, &msg);
if (ret == 0) {
@@ -103,12 +103,12 @@ int ldb_load_modules(struct ldb_context *ldb, const char *options[])
for (j = 0; j < msg[0]->num_elements; j++) {
for (k = 0; k < msg[0]->elements[j].num_values; k++) {
pn++;
- modules = ldb_realloc_array(ldb, modules, sizeof(char *), pn);
+ modules = talloc_realloc_p(ldb, modules, char *, pn);
if (!modules) {
ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in register_modules()\n");
return -1;
}
- modules[pn - 1] = ldb_strndup(ldb, msg[0]->elements[j].values[k].data, msg[0]->elements[j].values[k].length);
+ modules[pn - 1] = talloc_strndup(modules, msg[0]->elements[j].values[k].data, msg[0]->elements[j].values[k].length);
if (!modules[pn - 1]) {
ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in register_modules()\n");
return -1;
@@ -116,13 +116,11 @@ int ldb_load_modules(struct ldb_context *ldb, const char *options[])
}
}
}
- ldb_search_free(ldb, msg);
+ talloc_free(msg);
}
if (modules) {
-
for (i = 0; i < pn; i++) {
-
if (strcmp(modules[i], "timestamps") == 0) {
current = timestamps_module_init(ldb, options);
if (!current) {
@@ -274,10 +272,3 @@ const char *ldb_next_errstring(struct ldb_module *module)
return module->next->ops->errstring(module->next);
}
-void ldb_next_cache_free(struct ldb_module *module)
-{
- if (!module->next) {
- return;
- }
- module->next->ops->cache_free(module->next);
-}
diff --git a/source4/lib/ldb/common/ldb_msg.c b/source4/lib/ldb/common/ldb_msg.c
index 3865e7afa9..89f8feb3c0 100644
--- a/source4/lib/ldb/common/ldb_msg.c
+++ b/source4/lib/ldb/common/ldb_msg.c
@@ -36,6 +36,13 @@
#include "ldb/include/ldb.h"
#include "ldb/include/ldb_private.h"
+/*
+ create a new ldb_message in a given memory context (NULL for top level)
+*/
+struct ldb_message *ldb_msg_new(void *mem_ctx)
+{
+ return talloc_zero_p(mem_ctx, struct ldb_message);
+}
/*
find an element in a message by attribute name
@@ -88,7 +95,7 @@ struct ldb_val *ldb_msg_find_val(const struct ldb_message_element *el,
/*
duplicate a ldb_val structure
*/
-struct ldb_val ldb_val_dup(struct ldb_context *ldb,
+struct ldb_val ldb_val_dup(TALLOC_CTX *mem_ctx,
const struct ldb_val *v)
{
struct ldb_val v2;
@@ -100,7 +107,7 @@ struct ldb_val ldb_val_dup(struct ldb_context *ldb,
/* the +1 is to cope with buggy C library routines like strndup
that look one byte beyond */
- v2.data = ldb_malloc(ldb, v->length+1);
+ v2.data = talloc_array_p(mem_ctx, char, v->length+1);
if (!v2.data) {
v2.length = 0;
return v2;
@@ -119,8 +126,8 @@ int ldb_msg_add_empty(struct ldb_context *ldb,
{
struct ldb_message_element *els;
- els = ldb_realloc_p(ldb, msg->elements,
- struct ldb_message_element, msg->num_elements+1);
+ els = talloc_realloc_p(msg, msg->elements,
+ struct ldb_message_element, msg->num_elements+1);
if (!els) {
errno = ENOMEM;
return -1;
@@ -129,7 +136,7 @@ int ldb_msg_add_empty(struct ldb_context *ldb,
els[msg->num_elements].values = NULL;
els[msg->num_elements].num_values = 0;
els[msg->num_elements].flags = flags;
- els[msg->num_elements].name = ldb_strdup(ldb, attr_name);
+ els[msg->num_elements].name = talloc_strdup(els, attr_name);
if (!els[msg->num_elements].name) {
return -1;
}
@@ -178,7 +185,7 @@ int ldb_msg_add_value(struct ldb_context *ldb,
return -1;
}
- vals = ldb_realloc_p(ldb, el->values, struct ldb_val, el->num_values+1);
+ vals = talloc_realloc_p(msg, el->values, struct ldb_val, el->num_values+1);
if (!vals) {
errno = ENOMEM;
return -1;
@@ -332,19 +339,7 @@ void ldb_msg_sort_elements(struct ldb_message *msg)
*/
void ldb_msg_free(struct ldb_context *ldb, struct ldb_message *msg)
{
- int i, j;
-
- for (i=0;i<msg->num_elements;i++) {
- struct ldb_message_element *el = &msg->elements[i];
- for (j=0;j<el->num_values;j++) {
- ldb_free(ldb, el->values[j].data);
- }
- if (el->values) ldb_free(ldb, el->values);
- ldb_free(ldb, el->name);
- }
- if (msg->elements) ldb_free(ldb, msg->elements);
- ldb_free(ldb, msg->dn);
- ldb_free(ldb, msg);
+ talloc_free(msg);
}
/*
@@ -356,17 +351,17 @@ struct ldb_message *ldb_msg_copy(struct ldb_context *ldb,
struct ldb_message *msg2;
int i, j;
- msg2 = ldb_malloc_p(ldb, struct ldb_message);
+ msg2 = talloc_p(ldb, struct ldb_message);
if (msg2 == NULL) return NULL;
msg2->elements = NULL;
msg2->num_elements = 0;
msg2->private_data = NULL;
- msg2->dn = ldb_strdup(ldb, msg->dn);
+ msg2->dn = talloc_strdup(msg2, msg->dn);
if (msg2->dn == NULL) goto failed;
- msg2->elements = ldb_malloc_array_p(ldb, struct ldb_message_element, msg->num_elements);
+ msg2->elements = talloc_array_p(msg2, struct ldb_message_element, msg->num_elements);
if (msg2->elements == NULL) goto failed;
for (i=0;i<msg->num_elements;i++) {
@@ -376,15 +371,16 @@ struct ldb_message *ldb_msg_copy(struct ldb_context *ldb,
el2->flags = el1->flags;
el2->num_values = 0;
el2->values = NULL;
- el2->name = ldb_strdup(ldb, el1->name);
+ el2->name = talloc_strdup(msg2->elements, el1->name);
if (el2->name == NULL) goto failed;
- el2->values = ldb_malloc_array_p(ldb, struct ldb_val, el1->num_values);
+ el2->values = talloc_array_p(msg2->elements, struct ldb_val, el1->num_values);
for (j=0;j<el1->num_values;j++) {
el2->values[j] = ldb_val_dup(ldb, &el1->values[j]);
if (el2->values[j].data == NULL &&
el1->values[j].length != 0) {
goto failed;
}
+ el2->values[j].data = talloc_steal(el2->values, el2->values[j].data);
el2->num_values++;
}
@@ -394,7 +390,7 @@ struct ldb_message *ldb_msg_copy(struct ldb_context *ldb,
return msg2;
failed:
- ldb_msg_free(ldb, msg2);
+ talloc_free(msg2);
return NULL;
}
@@ -417,8 +413,8 @@ struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb,
struct ldb_message_element *el1 = &msg2->elements[i-1];
struct ldb_message_element *el2 = &msg2->elements[i];
if (ldb_msg_element_compare_name(el1, el2) == 0) {
- el1->values = ldb_realloc_p(ldb, el1->values, struct ldb_val,
- el1->num_values + el2->num_values);
+ el1->values = talloc_realloc_p(msg2->elements, el1->values, struct ldb_val,
+ el1->num_values + el2->num_values);
if (el1->values == NULL) {
return NULL;
}
@@ -426,8 +422,8 @@ struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb,
el2->values,
sizeof(struct ldb_val) * el2->num_values);
el1->num_values += el2->num_values;
- ldb_free(ldb, el2->name);
- ldb_free(ldb, el2->values);
+ talloc_free(el2->name);
+ talloc_free(el2->values);
if (i+1<msg2->num_elements) {
memmove(el2, el2+1, sizeof(struct ldb_message_element) *
(msg2->num_elements - (i+1)));
diff --git a/source4/lib/ldb/common/ldb_parse.c b/source4/lib/ldb/common/ldb_parse.c
index 1e00a05d55..6ee6f99253 100644
--- a/source4/lib/ldb/common/ldb_parse.c
+++ b/source4/lib/ldb/common/ldb_parse.c
@@ -65,7 +65,7 @@ a filter is defined by:
/*
return next token element. Caller frees
*/
-static char *ldb_parse_lex(struct ldb_context *ldb, const char **s, const char *sep)
+static char *ldb_parse_lex(TALLOC_CTX *ctx, const char **s, const char *sep)
{
const char *p = *s;
char *ret;
@@ -81,7 +81,7 @@ static char *ldb_parse_lex(struct ldb_context *ldb, const char **s, const char *
if (strchr(sep, *p)) {
(*s) = p+1;
- ret = ldb_strndup(ldb, p, 1);
+ ret = talloc_strndup(ctx, p, 1);
if (!ret) {
errno = ENOMEM;
}
@@ -96,7 +96,7 @@ static char *ldb_parse_lex(struct ldb_context *ldb, const char **s, const char *
return NULL;
}
- ret = ldb_strndup(ldb, *s, p - *s);
+ ret = talloc_strndup(ctx, *s, p - *s);
if (!ret) {
errno = ENOMEM;
}
@@ -128,47 +128,46 @@ static const char *match_brace(const char *s)
}
-static struct ldb_parse_tree *ldb_parse_filter(struct ldb_context *ldb, const char **s);
+static struct ldb_parse_tree *ldb_parse_filter(TALLOC_CTX *ctx, const char **s);
/*
<simple> ::= <attributetype> <filtertype> <attributevalue>
*/
-static struct ldb_parse_tree *ldb_parse_simple(struct ldb_context *ldb, const char *s)
+static struct ldb_parse_tree *ldb_parse_simple(TALLOC_CTX *ctx, const char *s)
{
char *eq, *val, *l;
struct ldb_parse_tree *ret;
- l = ldb_parse_lex(ldb, &s, LDB_ALL_SEP);
+ ret = talloc_p(ctx, struct ldb_parse_tree);
+ if (!ret) {
+ errno = ENOMEM;
+ return NULL;
+ }
+
+ l = ldb_parse_lex(ret, &s, LDB_ALL_SEP);
if (!l) {
+ talloc_free(ret);
return NULL;
}
if (strchr("()&|=", *l)) {
- ldb_free(ldb, l);
+ talloc_free(ret);
return NULL;
}
- eq = ldb_parse_lex(ldb, &s, LDB_ALL_SEP);
+ eq = ldb_parse_lex(ret, &s, LDB_ALL_SEP);
if (!eq || strcmp(eq, "=") != 0) {
- ldb_free(ldb, l);
- if (eq) ldb_free(ldb, eq);
+ talloc_free(ret);
return NULL;
}
- ldb_free(ldb, eq);
+ talloc_free(eq);
- val = ldb_parse_lex(ldb, &s, ")");
+ val = ldb_parse_lex(ret, &s, ")");
if (val && strchr("()&|", *val)) {
- ldb_free(ldb, l);
- if (val) ldb_free(ldb, val);
+ talloc_free(ret);
return NULL;
}
- ret = ldb_malloc_p(ldb, struct ldb_parse_tree);
- if (!ret) {
- errno = ENOMEM;
- return NULL;
- }
-
ret->operation = LDB_OP_SIMPLE;
ret->u.simple.attr = l;
ret->u.simple.value.data = val;
@@ -184,12 +183,12 @@ static struct ldb_parse_tree *ldb_parse_simple(struct ldb_context *ldb, const ch
<or> ::= '|' <filterlist>
<filterlist> ::= <filter> | <filter> <filterlist>
*/
-static struct ldb_parse_tree *ldb_parse_filterlist(struct ldb_context *ldb,
+static struct ldb_parse_tree *ldb_parse_filterlist(TALLOC_CTX *ctx,
enum ldb_parse_op op, const char *s)
{
struct ldb_parse_tree *ret, *next;
- ret = ldb_malloc_p(ldb, struct ldb_parse_tree);
+ ret = talloc_p(ctx, struct ldb_parse_tree);
if (!ret) {
errno = ENOMEM;
return NULL;
@@ -197,31 +196,29 @@ static struct ldb_parse_tree *ldb_parse_filterlist(struct ldb_context *ldb,
ret->operation = op;
ret->u.list.num_elements = 1;
- ret->u.list.elements = ldb_malloc_p(ldb, struct ldb_parse_tree *);
+ ret->u.list.elements = talloc_p(ret, struct ldb_parse_tree *);
if (!ret->u.list.elements) {
errno = ENOMEM;
- ldb_free(ldb, ret);
+ talloc_free(ret);
return NULL;
}
- ret->u.list.elements[0] = ldb_parse_filter(ldb, &s);
+ ret->u.list.elements[0] = ldb_parse_filter(ret->u.list.elements, &s);
if (!ret->u.list.elements[0]) {
- ldb_free(ldb, ret->u.list.elements);
- ldb_free(ldb, ret);
+ talloc_free(ret);
return NULL;
}
while (isspace(*s)) s++;
- while (*s && (next = ldb_parse_filter(ldb, &s))) {
+ while (*s && (next = ldb_parse_filter(ret->u.list.elements, &s))) {
struct ldb_parse_tree **e;
- e = ldb_realloc_p(ldb, ret->u.list.elements,
- struct ldb_parse_tree *,
- ret->u.list.num_elements+1);
+ e = talloc_realloc_p(ret, ret->u.list.elements,
+ struct ldb_parse_tree *,
+ ret->u.list.num_elements+1);
if (!e) {
errno = ENOMEM;
- ldb_parse_tree_free(ldb, next);
- ldb_parse_tree_free(ldb, ret);
+ talloc_free(ret);
return NULL;
}
ret->u.list.elements = e;
@@ -237,20 +234,20 @@ static struct ldb_parse_tree *ldb_parse_filterlist(struct ldb_context *ldb,
/*
<not> ::= '!' <filter>
*/
-static struct ldb_parse_tree *ldb_parse_not(struct ldb_context *ldb, const char *s)
+static struct ldb_parse_tree *ldb_parse_not(TALLOC_CTX *ctx, const char *s)
{
struct ldb_parse_tree *ret;
- ret = ldb_malloc_p(ldb, struct ldb_parse_tree);
+ ret = talloc_p(ctx, struct ldb_parse_tree);
if (!ret) {
errno = ENOMEM;
return NULL;
}
ret->operation = LDB_OP_NOT;
- ret->u.not.child = ldb_parse_filter(ldb, &s);
+ ret->u.not.child = ldb_parse_filter(ret, &s);
if (!ret->u.not.child) {
- ldb_free(ldb, ret);
+ talloc_free(ret);
return NULL;
}
@@ -261,49 +258,48 @@ static struct ldb_parse_tree *ldb_parse_not(struct ldb_context *ldb, const char
parse a filtercomp
<filtercomp> ::= <and> | <or> | <not> | <simple>
*/
-static struct ldb_parse_tree *ldb_parse_filtercomp(struct ldb_context *ldb,
- const char *s)
+static struct ldb_parse_tree *ldb_parse_filtercomp(TALLOC_CTX *ctx, const char *s)
{
while (isspace(*s)) s++;
switch (*s) {
case '&':
- return ldb_parse_filterlist(ldb, LDB_OP_AND, s+1);
+ return ldb_parse_filterlist(ctx, LDB_OP_AND, s+1);
case '|':
- return ldb_parse_filterlist(ldb, LDB_OP_OR, s+1);
+ return ldb_parse_filterlist(ctx, LDB_OP_OR, s+1);
case '!':
- return ldb_parse_not(ldb, s+1);
+ return ldb_parse_not(ctx, s+1);
case '(':
case ')':
return NULL;
}
- return ldb_parse_simple(ldb, s);
+ return ldb_parse_simple(ctx, s);
}
/*
<filter> ::= '(' <filtercomp> ')'
*/
-static struct ldb_parse_tree *ldb_parse_filter(struct ldb_context *ldb, const char **s)
+static struct ldb_parse_tree *ldb_parse_filter(TALLOC_CTX *ctx, const char **s)
{
char *l, *s2;
const char *p, *p2;
struct ldb_parse_tree *ret;
- l = ldb_parse_lex(ldb, s, LDB_ALL_SEP);
+ l = ldb_parse_lex(ctx, s, LDB_ALL_SEP);
if (!l) {
return NULL;
}
if (strcmp(l, "(") != 0) {
- ldb_free(ldb, l);
+ talloc_free(l);
return NULL;
}
- ldb_free(ldb, l);
+ talloc_free(l);
p = match_brace(*s);
if (!p) {
@@ -311,14 +307,14 @@ static struct ldb_parse_tree *ldb_parse_filter(struct ldb_context *ldb, const ch
}
p2 = p + 1;
- s2 = ldb_strndup(ldb, *s, p - *s);
+ s2 = talloc_strndup(ctx, *s, p - *s);
if (!s2) {
errno = ENOMEM;
return NULL;
}
- ret = ldb_parse_filtercomp(ldb, s2);
- ldb_free(ldb, s2);
+ ret = ldb_parse_filtercomp(ctx, s2);
+ talloc_free(s2);
*s = p2;
@@ -347,27 +343,6 @@ struct ldb_parse_tree *ldb_parse_tree(struct ldb_context *ldb, const char *s)
*/
void ldb_parse_tree_free(struct ldb_context *ldb, struct ldb_parse_tree *tree)
{
- unsigned int i;
-
- switch (tree->operation) {
- case LDB_OP_SIMPLE:
- ldb_free(ldb, tree->u.simple.attr);
- if (tree->u.simple.value.data) ldb_free(ldb, tree->u.simple.value.data);
- break;
-
- case LDB_OP_AND:
- case LDB_OP_OR:
- for (i=0;i<tree->u.list.num_elements;i++) {
- ldb_parse_tree_free(ldb, tree->u.list.elements[i]);
- }
- if (tree->u.list.elements) ldb_free(ldb, tree->u.list.elements);
- break;
-
- case LDB_OP_NOT:
- ldb_parse_tree_free(ldb, tree->u.not.child);
- break;
- }
-
- ldb_free(ldb, tree);
+ talloc_free(tree);
}
diff --git a/source4/lib/ldb/common/ldb_utf8.c b/source4/lib/ldb/common/ldb_utf8.c
index 01bd8eb9ac..577766d9f7 100644
--- a/source4/lib/ldb/common/ldb_utf8.c
+++ b/source4/lib/ldb/common/ldb_utf8.c
@@ -44,7 +44,7 @@
char *ldb_casefold(struct ldb_context *ldb, const char *s)
{
int i;
- char *ret = ldb_strdup(ldb, s);
+ char *ret = talloc_strdup(ldb, s);
if (!s) {
errno = ENOMEM;
return NULL;