From fa29eed6810844dee1ca481a74ab80810baeda6b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pavel=20Filipensk=C3=BD?= Date: Wed, 27 Jul 2022 17:40:03 +0200 Subject: [PATCH] lib:util: Add BURN_FREE() and BURN_FREE_STR() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Pavel Filipenský Reviewed-by: Andreas Schneider --- lib/util/memory.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/util/memory.h b/lib/util/memory.h index 4f7986c9b0c..40c66d824a1 100644 --- a/lib/util/memory.h +++ b/lib/util/memory.h @@ -31,6 +31,33 @@ #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0) #endif +/** + * Zero string and free memory if the pointer and zero the pointer. + * + * @note You are explicitly allowed to pass NULL pointers -- they will + * always be ignored. + **/ +#define BURN_FREE_STR(x) do { \ + if ((x) != NULL) { \ + size_t s = strlen(x); \ + memset_s((x), s, 0, s); \ + free(x); (x) = NULL; \ + } \ + } while(0) + +/** + * Zero and free memory if the pointer and zero the pointer. + * + * @note You are explicitly allowed to pass NULL pointers -- they will + * always be ignored. + **/ +#define BURN_FREE(x, s) do { \ + if ((x) != NULL) { \ + memset_s((x), (s), 0, (s)); \ + free(x); (x) = NULL; \ + } \ + } while(0) + /** * Type-safe version of malloc. Allocated one copy of the * specified data type. -- 2.47.3