Merge pull request #12965 from keszybz/auto-erase

_cleanup_(erase_and_freep)
This commit is contained in:
Lennart Poettering
2019-07-10 16:20:50 +02:00
committed by GitHub
7 changed files with 86 additions and 97 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));