From fafc3c2d5c7fae6bad0f6dc51611ae9390589ade Mon Sep 17 00:00:00 2001 From: Adrian Vovk Date: Wed, 4 Sep 2024 13:44:26 -0400 Subject: [PATCH] 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 --- src/basic/alloc-util.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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) \ ({ \ -- 2.47.3