From 1a9957cf23b3635f60dd22485988fe47d7154f6c Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Tue, 2 Jun 2009 15:26:13 -0400 Subject: Turn sssd_mem_takeover into sssd_mem_attach The old function was not used anywhere, and this function uses better semantics, including not using void ** which gives strict aliasing problems. Also add a generic password destroy function --- server/util/memory.c | 51 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-) (limited to 'server/util/memory.c') diff --git a/server/util/memory.c b/server/util/memory.c index 87fefd2f..8ffad49a 100644 --- a/server/util/memory.c +++ b/server/util/memory.c @@ -1,30 +1,47 @@ #include "talloc.h" +#include "util/util.h" /* - * sssd_mem_takeover - * This function will take a non-talloc pointer and add it to a talloc + * sssd_mem_attach + * This function will take a non-talloc pointer and "attach" it to a talloc * memory context. It will accept a destructor for the original pointer * so that when the parent memory context is freed, the non-talloc * pointer will also be freed properly. */ -TALLOC_CTX *sssd_mem_takeover(TALLOC_CTX *mem_ctx, - void *ptr, - int (*destructor)(void **)) + +int password_destructor(void *memctx) +{ + char *password = (char *)memctx; + int i; + + /* zero out password */ + for (i = 0; password[i]; i++) password[i] = '\0'; + + return 0; +} + +static int mem_holder_destructor(void *ptr) +{ + struct mem_holder *h; + + h = talloc_get_type(ptr, struct mem_holder); + return h->fn(h->mem); +} + +void *sss_mem_attach(TALLOC_CTX *mem_ctx, + void *ptr, + void_destructor_fn_t *fn) { - TALLOC_CTX **handle; + struct mem_holder *h; - if (ptr == NULL) { - return NULL; - } + if (!ptr || !fn) return NULL; - handle = talloc_named_const(mem_ctx, sizeof(void *), - "sssd_mem_takeover destructor handle"); - if (handle == NULL) { - return NULL; - } + h = talloc(mem_ctx, struct mem_holder); + if (!h) return NULL; - *handle = ptr; - talloc_set_destructor(handle, destructor); + h->mem = ptr; + h->fn = fn; + talloc_set_destructor((TALLOC_CTX *)h, mem_holder_destructor); - return handle; + return h; } -- cgit