1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2024-11-12 22:45:54 +05:30
gists/c-programming/mem/safe_mem.test.c

66 lines
2.0 KiB
C

/*
* safe_mem.test.c
*
* Author: Intel A80486DX2-66
* License: Unlicense
*/
#include <stdio.h>
#include <stdlib.h>
#include "safe_mem.h"
#undef SAFE_FREE_ERROR_HOOK
#define SAFE_FREE_ERROR_HOOK do { \
fprintf(stderr, "Double freeing detected!\n"); \
exit(EXIT_FAILURE); \
} while (0)
#define PRINT_POINTER(caption, ptr) \
printf(caption " = %p\n", (void*) ptr);
#define DUMP_INT_ARRAY(caption, ptr, count, fmt) do { \
printf("%s: [", caption); \
for (size_t i = 0; i < count; i++) { \
printf(fmt, ptr[i]); \
if (i != (count - 1)) \
printf(", "); \
} \
printf("]\n"); \
} while (0)
int main(void) {
#define COUNT 4
int* integers = precise_malloc(COUNT, sizeof(int));
PRINT_POINTER("int* integers (before freeing)", integers);
integers[0] = 0;
integers[1] = EOF;
integers[2] = 2;
integers[3] = 1;
DUMP_INT_ARRAY("int* integers (before secure erasure)", integers, COUNT,
"%d");
printf("Secure erasure is "
#if NO_SECURE_ERASE_WARRANTY
"not guaranteed!"
#else
"guaranteed: %s"
#endif
"\n"
#if !NO_SECURE_ERASE_WARRANTY
, SECURE_ERASE_WARRANTY
#endif
);
secure_erase(integers, COUNT * sizeof(int));
DUMP_INT_ARRAY("int* integers (after secure erasure)", integers, COUNT,
"%d");
safe_free(integers);
PRINT_POINTER("int* integers (after freeing)", integers);
fflush(stdout);
safe_free(integers);
PRINT_POINTER("int* integers (after two free() calls)", integers);
return EXIT_SUCCESS;
}