From: Adrian Vovk Date: Wed, 4 Sep 2024 17:44:26 +0000 (-0400) Subject: GREEDY_REALLOC_APPEND: Make more type safe X-Git-Tag: v257-rc1~192 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fafc3c2d5c7fae6bad0f6dc51611ae9390589ade;p=thirdparty%2Fsystemd.git GREEDY_REALLOC_APPEND: Make more type safe 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 --- diff --git a/src/basic/alloc-util.h b/src/basic/alloc-util.h index 462092703aa..ba71298287a 100644 --- a/src/basic/alloc-util.h +++ b/src/basic/alloc-util.h @@ -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) \ ({ \