summaryrefslogtreecommitdiffstats
path: root/server/util/memory.c
diff options
context:
space:
mode:
authorSimo Sorce <ssorce@redhat.com>2009-06-02 15:26:13 -0400
committerSimo Sorce <ssorce@redhat.com>2009-06-10 14:55:20 -0400
commit1a9957cf23b3635f60dd22485988fe47d7154f6c (patch)
tree3515e84d82392f6f12123787f08aadb201a5c46b /server/util/memory.c
parent1bbaf1a7cfde76bd81cab964c2eb9c91c6d8feba (diff)
downloadsssd-1a9957cf23b3635f60dd22485988fe47d7154f6c.tar.gz
sssd-1a9957cf23b3635f60dd22485988fe47d7154f6c.tar.xz
sssd-1a9957cf23b3635f60dd22485988fe47d7154f6c.zip
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
Diffstat (limited to 'server/util/memory.c')
-rw-r--r--server/util/memory.c51
1 files changed, 34 insertions, 17 deletions
diff --git a/server/util/memory.c b/server/util/memory.c
index 87fefd2f2..8ffad49aa 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;
}