]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
alloc-util: add realloc0() helper than is like realloc() but zero-initializes appende...
authorLennart Poettering <lennart@poettering.net>
Fri, 22 Sep 2023 20:22:12 +0000 (22:22 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 6 Oct 2023 05:44:47 +0000 (07:44 +0200)
src/basic/alloc-util.h

index 156c973e6920c1f62f4d6c87e0897502dfbd2cee..3ef834aceee5d06741cf0dda3f6dc22ea2b1a9f0 100644 (file)
@@ -254,4 +254,24 @@ static inline void free_many_charp(char **c, size_t n) {
         free_many((void**) c, n);
 }
 
+_alloc_(2) static inline void *realloc0(void *p, size_t new_size) {
+        size_t old_size;
+        void *q;
+
+        /* Like realloc(), but initializes anything appended to zero */
+
+        old_size = MALLOC_SIZEOF_SAFE(p);
+
+        q = realloc(p, new_size);
+        if (!q)
+                return NULL;
+
+        new_size = MALLOC_SIZEOF_SAFE(q); /* Update with actually allocated space */
+
+        if (new_size > old_size)
+                memset((uint8_t*) q + old_size, 0, new_size - old_size);
+
+        return q;
+}
+
 #include "memory-util.h"