From: Pavel Filipenský Date: Wed, 27 Jul 2022 15:40:03 +0000 (+0200) Subject: lib:util: Add BURN_FREE() and BURN_FREE_STR() X-Git-Tag: talloc-2.4.0~1329 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fa29eed6810844dee1ca481a74ab80810baeda6b;p=thirdparty%2Fsamba.git lib:util: Add BURN_FREE() and BURN_FREE_STR() Signed-off-by: Pavel Filipenský Reviewed-by: Andreas Schneider --- 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.