]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib:util: Add BURN_FREE() and BURN_FREE_STR()
authorPavel Filipenský <pfilipensky@samba.org>
Wed, 27 Jul 2022 15:40:03 +0000 (17:40 +0200)
committerAndreas Schneider <asn@cryptomilk.org>
Fri, 26 Aug 2022 07:59:32 +0000 (07:59 +0000)
Signed-off-by: Pavel Filipenský <pfilipensky@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
lib/util/memory.h

index 4f7986c9b0c50bf0f310487077cf38a958f66017..40c66d824a1cf60e4c65981d4c8ce500c44feb61 100644 (file)
 #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.