]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
GREEDY_REALLOC_APPEND: Make more type safe
authorAdrian Vovk <adrianvovk@gmail.com>
Wed, 4 Sep 2024 17:44:26 +0000 (13:44 -0400)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 18 Oct 2024 05:22:58 +0000 (14:22 +0900)
Previously, GREEDY_REALLOC_APPEND would compile perfectly fine and cause
subtle memory corruption if the caller messes up the type they're passing
in (i.e. by forgetting to pass-by-reference when appending a Type* to an
array of Type*). Now this will lead to compilation failure

src/basic/alloc-util.h

index 462092703aa5f0d4e89a93feb68195a1d9569ea1..ba71298287addec7054dba3560d16f04e465e4a0 100644 (file)
@@ -155,7 +155,10 @@ void* greedy_realloc_append(void **p, size_t *n_p, const void *from, size_t n_fr
         greedy_realloc0((void**) &(array), (need), sizeof((array)[0]))
 
 #define GREEDY_REALLOC_APPEND(array, n_array, from, n_from)             \
-        greedy_realloc_append((void**) &(array), (size_t*) &(n_array), (from), (n_from), sizeof((array)[0]))
+        ({                                                              \
+                const typeof(*(array)) *_from_ = (from);                \
+                greedy_realloc_append((void**) &(array), &(n_array), _from_, (n_from), sizeof((array)[0])); \
+        })
 
 #define alloca0(n)                                      \
         ({                                              \