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:
parent
7810d22171
commit
44c786f04a
|
@ -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));
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include "alloc-util.h"
|
||||
#include "macro.h"
|
||||
#include "memory-util.h"
|
||||
#include "random-util.h"
|
||||
#include "tests.h"
|
||||
|
||||
static void test_alloca(void) {
|
||||
|
@ -132,6 +133,20 @@ static void test_cleanup_order(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);
|
||||
|
||||
|
@ -140,6 +155,7 @@ int main(int argc, char *argv[]) {
|
|||
test_memdup_multiply_and_greedy_realloc();
|
||||
test_bool_assign();
|
||||
test_cleanup_order();
|
||||
test_auto_erase_memory();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue