test: add _cleanup_(erase_and_freep)

Based on the macro and test case by Lennart Poettering and
Topi Miettinen suggestion.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2019-07-10 11:39:03 +02:00
parent 7810d22171
commit 44c786f04a
2 changed files with 27 additions and 0 deletions
+11
View File
@@ -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));