2024-03-24 18:24:04 +05:30
|
|
|
/*
|
2024-03-24 18:28:19 +05:30
|
|
|
* safe_mem.test.c
|
2024-03-24 18:24:04 +05:30
|
|
|
*
|
|
|
|
* Author: Intel A80486DX2-66
|
|
|
|
* License: Creative Commons Zero 1.0 Universal
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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) do { \
|
|
|
|
printf("%s: [", caption); \
|
|
|
|
for (size_t i = 0; i < count; i++) { \
|
|
|
|
printf("%d", 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;
|
2024-03-24 20:38:38 +05:30
|
|
|
integers[2] = 2;
|
2024-03-24 18:24:04 +05:30
|
|
|
integers[3] = 1;
|
|
|
|
DUMP_INT_ARRAY("int* integers (before secure erasure)", integers, COUNT);
|
|
|
|
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);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|