From 282bde106652057c5d278a13dd3a0044ca5a9765 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 8 Aug 2019 19:53:17 +0200 Subject: [PATCH] memory-util: introduce erase_and_free() helper --- src/basic/memory-util.h | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/basic/memory-util.h b/src/basic/memory-util.h index 46a6907a0cf..4f92a6434a5 100644 --- a/src/basic/memory-util.h +++ b/src/basic/memory-util.h @@ -80,14 +80,21 @@ static inline void* explicit_bzero_safe(void *p, size_t l) { void *explicit_bzero_safe(void *p, size_t l); #endif -static inline void erase_and_freep(void *p) { - void *ptr = *(void**) p; +static inline void* erase_and_free(void *p) { + size_t l; - if (ptr) { - size_t l = malloc_usable_size(ptr); - explicit_bzero_safe(ptr, l); - free(ptr); - } + if (!p) + return NULL; + + l = malloc_usable_size(p); + explicit_bzero_safe(p, l); + free(p); + + return NULL; +} + +static inline void erase_and_freep(void *p) { + erase_and_free(*(void**) p); } /* Use with _cleanup_ to erase a single 'char' when leaving scope */