diff --git a/source4/ldap_server/ldap_simple_ldb.c b/source4/ldap_server/ldap_simple_ldb.c index d4c576a189d..7c6341b872a 100644 --- a/source4/ldap_server/ldap_simple_ldb.c +++ b/source4/ldap_server/ldap_simple_ldb.c @@ -141,7 +141,7 @@ static NTSTATUS sldb_Search(struct ldapsrv_partition *partition, struct ldapsrv_ attrs[i] = NULL; } - ldb_set_alloc(ldb, talloc_ldb_alloc, samdb); + ldb_set_alloc(ldb, talloc_realloc_fn, samdb); count = ldb_search(ldb, basedn, scope, r->filter, attrs, &res); for (i=0; i < count; i++) { @@ -284,7 +284,7 @@ invalid_input: add_result->dn = talloc_steal(add_reply, dn); if (result == LDAP_SUCCESS) { - ldb_set_alloc(ldb, talloc_ldb_alloc, samdb); + ldb_set_alloc(ldb, talloc_realloc_fn, samdb); ldb_ret = ldb_add(ldb, msg); if (ldb_ret == 0) { result = LDAP_SUCCESS; @@ -330,7 +330,7 @@ static NTSTATUS sldb_Del(struct ldapsrv_partition *partition, struct ldapsrv_cal DEBUG(10, ("sldb_Del: dn: [%s]\n", dn)); - ldb_set_alloc(ldb, talloc_ldb_alloc, samdb); + ldb_set_alloc(ldb, talloc_realloc_fn, samdb); ldb_ret = ldb_delete(ldb, dn); del_reply = ldapsrv_init_reply(call, LDAP_TAG_DelResponse); @@ -449,7 +449,7 @@ invalid_input: modify_result->dn = talloc_steal(modify_reply, dn); if (result == LDAP_SUCCESS) { - ldb_set_alloc(ldb, talloc_ldb_alloc, samdb); + ldb_set_alloc(ldb, talloc_realloc_fn, samdb); ldb_ret = ldb_modify(ldb, msg); if (ldb_ret == 0) { result = LDAP_SUCCESS; @@ -503,7 +503,7 @@ static NTSTATUS sldb_Compare(struct ldapsrv_partition *partition, struct ldapsrv attrs[0] = NULL; - ldb_set_alloc(ldb, talloc_ldb_alloc, samdb); + ldb_set_alloc(ldb, talloc_realloc_fn, samdb); count = ldb_search(ldb, dn, LDB_SCOPE_BASE, filter, attrs, &res); compare_r = ldapsrv_init_reply(call, LDAP_TAG_CompareResponse); diff --git a/source4/lib/ldb/common/ldb_alloc.c b/source4/lib/ldb/common/ldb_alloc.c index 6abd6fa8c3c..0053c5e73fa 100644 --- a/source4/lib/ldb/common/ldb_alloc.c +++ b/source4/lib/ldb/common/ldb_alloc.c @@ -39,7 +39,7 @@ this allows the user to choose their own allocation function */ int ldb_set_alloc(struct ldb_context *ldb, - void *(*alloc)(void *context, void *ptr, size_t size), + void *(*alloc)(const void *context, void *ptr, size_t size), void *context) { ldb->alloc_ops.alloc = alloc; @@ -50,7 +50,7 @@ int ldb_set_alloc(struct ldb_context *ldb, /* this is the default memory allocation function */ -static void *ldb_default_alloc(void *context, void *ptr, size_t size) +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 diff --git a/source4/lib/ldb/include/ldb.h b/source4/lib/ldb/include/ldb.h index 241f1869912..4b1401f6735 100644 --- a/source4/lib/ldb/include/ldb.h +++ b/source4/lib/ldb/include/ldb.h @@ -145,7 +145,7 @@ struct ldb_backend_ops { for pool allocators */ struct ldb_alloc_ops { - void *(*alloc)(void *context, void *ptr, size_t size); + void *(*alloc)(const void *context, void *ptr, size_t size); void *context; }; @@ -320,7 +320,7 @@ const char *ldb_msg_find_string(const struct ldb_message *msg, which often take a context argument */ int ldb_set_alloc(struct ldb_context *ldb, - void *(*alloc)(void *context, void *ptr, size_t size), + void *(*alloc)(const void *context, void *ptr, size_t size), void *context); /* diff --git a/source4/lib/talloc.c b/source4/lib/talloc.c index fc655460638..5c1f3e7dcd0 100644 --- a/source4/lib/talloc.c +++ b/source4/lib/talloc.c @@ -273,6 +273,10 @@ int talloc_unlink(const void *context, void *ptr) struct talloc_chunk *tc_p, *new_p; void *new_parent; + if (context == NULL) { + context = null_context; + } + if (talloc_unreference(context, ptr) == 0) { return 0; } @@ -561,6 +565,10 @@ void *talloc_steal(const void *new_ctx, const void *ptr) return NULL; } + if (new_ctx == NULL) { + new_ctx = null_context; + } + tc = talloc_chunk_from_ptr(ptr); if (new_ctx == NULL) { @@ -948,16 +956,11 @@ void *talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned } /* - a alloc function for ldb that uses talloc + a function version of talloc_realloc(), so it can be passed as a function pointer + to libraries that want a realloc function (a realloc function encapsulates + all the basic capabilities of an allocation library, which is why this is useful) */ -void *talloc_ldb_alloc(void *context, void *ptr, size_t size) +void *talloc_realloc_fn(const void *context, void *ptr, size_t size) { - if (ptr == NULL) { - return talloc(context, size); - } - if (size == 0) { - talloc_free(ptr); - return NULL; - } - return talloc_realloc(context, ptr, size); + return _talloc_realloc(context, ptr, size, NULL); } diff --git a/source4/rpc_server/common/gendb.c b/source4/rpc_server/common/gendb.c index d20aef5ff87..0e6ddb0693b 100644 --- a/source4/rpc_server/common/gendb.c +++ b/source4/rpc_server/common/gendb.c @@ -42,7 +42,7 @@ int gendb_search_v(struct ldb_context *ldb, return -1; } - ldb_set_alloc(ldb, talloc_ldb_alloc, mem_ctx); + ldb_set_alloc(ldb, talloc_realloc_fn, mem_ctx); count = ldb_search(ldb, basedn, LDB_SCOPE_SUBTREE, expr, attrs, res); diff --git a/source4/rpc_server/netlogon/schannel_state.c b/source4/rpc_server/netlogon/schannel_state.c index f64aabb1bf1..88eabc6d379 100644 --- a/source4/rpc_server/netlogon/schannel_state.c +++ b/source4/rpc_server/netlogon/schannel_state.c @@ -43,7 +43,7 @@ static struct ldb_context *schannel_db_connect(TALLOC_CTX *mem_ctx) return NULL; } - ldb_set_alloc(ldb, talloc_ldb_alloc, mem_ctx); + ldb_set_alloc(ldb, talloc_realloc_fn, mem_ctx); return ldb; } diff --git a/source4/rpc_server/samr/samdb.c b/source4/rpc_server/samr/samdb.c index 1628919f4e8..330741e29d4 100644 --- a/source4/rpc_server/samr/samdb.c +++ b/source4/rpc_server/samr/samdb.c @@ -119,7 +119,7 @@ int samdb_search_free(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message **res) { struct samdb_context *sam_ctx = ctx; - ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx); + ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx); return ldb_search_free(sam_ctx->ldb, res); } @@ -746,7 +746,7 @@ int samdb_msg_add_string(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg if (s == NULL || a == NULL) { return -1; } - ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx); + ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx); return ldb_msg_add_string(sam_ctx->ldb, msg, a, s); } @@ -761,7 +761,7 @@ int samdb_msg_add_delete(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg if (a == NULL) { return -1; } - ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx); + ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx); /* we use an empty replace rather than a delete, as it allows for samdb_replace() to be used everywhere */ return ldb_msg_add_empty(sam_ctx->ldb, msg, a, LDB_FLAG_MOD_REPLACE); @@ -811,7 +811,7 @@ int samdb_msg_add_hash(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg, return -1; } memcpy(val.data, hash.hash, 16); - ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx); + ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx); return ldb_msg_add_value(sam_ctx->ldb, msg, attr_name, &val); } @@ -832,7 +832,7 @@ int samdb_msg_add_hashes(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg for (i=0;ildb, talloc_ldb_alloc, mem_ctx); + ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx); return ldb_msg_add_value(sam_ctx->ldb, msg, attr_name, &val); } @@ -855,7 +855,7 @@ int samdb_msg_add_logon_hours(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message struct ldb_val val; val.length = hours.units_per_week / 8; val.data = hours.bitmap; - ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx); + ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx); return ldb_msg_add_value(sam_ctx->ldb, msg, attr_name, &val); } @@ -868,7 +868,7 @@ int samdb_msg_set_string(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg struct samdb_context *sam_ctx = ctx; struct ldb_message_element *el; - ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx); + ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx); el = ldb_msg_find_element(msg, attr_name); if (el) { @@ -897,7 +897,7 @@ int samdb_add(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg) { struct samdb_context *sam_ctx = ctx; - ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx); + ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx); return ldb_add(sam_ctx->ldb, msg); } @@ -908,7 +908,7 @@ int samdb_delete(void *ctx, TALLOC_CTX *mem_ctx, const char *dn) { struct samdb_context *sam_ctx = ctx; - ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx); + ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx); return ldb_delete(sam_ctx->ldb, dn); } @@ -919,7 +919,7 @@ int samdb_modify(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg) { struct samdb_context *sam_ctx = ctx; - ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx); + ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx); return ldb_modify(sam_ctx->ldb, msg); } diff --git a/talloc_guide.txt b/talloc_guide.txt index 7742ade6235..484b4c82285 100644 --- a/talloc_guide.txt +++ b/talloc_guide.txt @@ -458,3 +458,13 @@ The talloc_realloc_p() macro is equivalent to: except that it provides integer overflow protection for the multiply, returning NULL if the multiply overflows. + +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +void *talloc_realloc_fn(const void *ctx, void *ptr, size_t size); + +This is a non-macro version of talloc_realloc(), which is useful +as libraries sometimes want a ralloc function pointer. A realloc() +implementation encapsulates the functionality of malloc(), free() and +realloc() in one call, which is why it is useful to be able to pass +around a single function pointer. +