diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2019-07-05 10:37:53 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2019-07-10 11:39:03 +0200 |
commit | 44c786f04ad40d89b0a0cf4c31612b0b2a4f3765 (patch) | |
tree | ae56a0575ae01219401086ac92335ecc8e5b839c /src/basic/memory-util.h | |
parent | Merge pull request #12946 from poettering/blockdev-tweaks (diff) | |
download | systemd-44c786f04ad40d89b0a0cf4c31612b0b2a4f3765.tar.xz systemd-44c786f04ad40d89b0a0cf4c31612b0b2a4f3765.zip |
test: add _cleanup_(erase_and_freep)
Based on the macro and test case by Lennart Poettering and
Topi Miettinen suggestion.
Diffstat (limited to 'src/basic/memory-util.h')
-rw-r--r-- | src/basic/memory-util.h | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/src/basic/memory-util.h b/src/basic/memory-util.h index 0e8957b783..9cb8ac3c10 100644 --- a/src/basic/memory-util.h +++ b/src/basic/memory-util.h @@ -2,6 +2,7 @@ #pragma once #include <inttypes.h> +#include <malloc.h> #include <stdbool.h> #include <string.h> #include <sys/types.h> @@ -78,6 +79,16 @@ 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; + + if (ptr) { + size_t l = malloc_usable_size(ptr); + explicit_bzero_safe(ptr, l); + free(ptr); + } +} + /* Use with _cleanup_ to erase a single 'char' when leaving scope */ static inline void erase_char(char *p) { explicit_bzero_safe(p, sizeof(char)); |