#pragma once
#include <inttypes.h>
+#include <malloc.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
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));
#include "alloc-util.h"
#include "macro.h"
#include "memory-util.h"
+#include "random-util.h"
#include "tests.h"
static void test_alloca(void) {
log_debug("z: %p", &z);
}
+static void test_auto_erase_memory(void) {
+ _cleanup_(erase_and_freep) uint8_t *p1, *p2;
+
+ assert_se(p1 = new(uint8_t, 1024));
+ assert_se(p2 = new(uint8_t, 1024));
+
+ genuine_random_bytes(p1, 1024, RANDOM_BLOCK);
+
+ /* before we exit the scope, do something with this data, so that the compiler won't optimize this away */
+ memcpy(p2, p1, 1024);
+ for (size_t i = 0; i < 1024; i++)
+ assert_se(p1[i] == p2[i]);
+}
+
int main(int argc, char *argv[]) {
test_setup_logging(LOG_DEBUG);
test_memdup_multiply_and_greedy_realloc();
test_bool_assign();
test_cleanup_order();
+ test_auto_erase_memory();
return 0;
}