1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2025-01-13 09:42:05 +05:30

safe_mem.h: fix secure_erase macro

This commit is contained in:
パチュリー・ノーレッジ 2024-03-24 21:04:19 +03:00
parent 471e2a8583
commit f4fbea4850
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B

View File

@ -12,6 +12,8 @@
#include <stdlib.h>
typedef unsigned char byte;
#define SAFE_FREE_ERROR_HOOK /* user-defined */
#define safe_free(ptr) do { \
@ -24,7 +26,7 @@
#define precise_malloc(nmemb, size) \
/* prevents incorrect casting */ \
malloc((size_t) nmemb * (size_t) size)
malloc((size_t) (nmemb) * (size_t) (size))
/* secure_erase(dest, count): erases memory explicitly */
#ifdef __FreeBSD__
@ -44,24 +46,16 @@
# define SECURE_ERASE_WARRANTY "C11+: memset_s"
# include <string.h>
# define secure_erase(dest, count) memset_s(dest, count, 0, count)
# define secure_erase(dest, count) memset_s((dest), (count), 0, (count))
#else
# define NO_SECURE_ERASE_WARRANTY 1
# include <string.h>
# define __observe__(ptr) do { \
if (*ptr == 0) { \
++(*ptr); \
fprintf(stderr, ""); \
} else \
--(*ptr); \
} while (0)
# define secure_erase(dest, count) do { \
memset(dest, 0, count); \
for (size_t i = 0; i < count; i++) \
__observe__(dest[i]); \
uintptr_t max = (uintptr_t) ((count) / sizeof(byte)); \
for (size_t i = 0; i < max; i++) \
*((byte*) (dest) + i) = 0; \
} while (0)
#endif