about summary refs log tree commit diff
path: root/shared/systemd/src/basic/memory-util.h
diff options
context:
space:
mode:
Diffstat (limited to 'shared/systemd/src/basic/memory-util.h')
-rw-r--r--shared/systemd/src/basic/memory-util.h15
1 files changed, 13 insertions, 2 deletions
diff --git a/shared/systemd/src/basic/memory-util.h b/shared/systemd/src/basic/memory-util.h
index 915c24a5..9cb8ac3c 100644
--- a/shared/systemd/src/basic/memory-util.h
+++ b/shared/systemd/src/basic/memory-util.h
@@ -2,6 +2,7 @@
 #pragma once
 
 #include <inttypes.h>
+#include <malloc.h>
 #include <stdbool.h>
 #include <string.h>
 #include <sys/types.h>
@@ -37,8 +38,8 @@ static inline int memcmp_nn(const void *s1, size_t n1, const void *s2, size_t n2
 #define memzero(x,l)                                            \
         ({                                                      \
                 size_t _l_ = (l);                               \
-                void *_x_ = (x);                                \
-                _l_ == 0 ? _x_ : memset(_x_, 0, _l_);           \
+                if (_l_ > 0)                                    \
+                        memset(x, 0, _l_);                      \
         })
 
 #define zero(x) (memzero(&(x), sizeof(x)))
@@ -78,6 +79,16 @@ static inline void* explicit_bzero_safe(void *p, size_t l) {
 void *explicit_bzero_safe(void *p, size_t l);
 #endif
 
+static inline void erase_and_freep(void *p) {
+        void *ptr = *(void**) p;
+
+        if (ptr) {
+                size_t l = malloc_usable_size(ptr);
+                explicit_bzero_safe(ptr, l);
+                free(ptr);
+        }
+}
+
 /* Use with _cleanup_ to erase a single 'char' when leaving scope */
 static inline void erase_char(char *p) {
         explicit_bzero_safe(p, sizeof(char));