summaryrefslogtreecommitdiffstats
path: root/src/basic/alloc-util.h
diff options
context:
space:
mode:
authorAdrian Vovk <adrianvovk@gmail.com>2024-09-04 19:44:26 +0200
committerYu Watanabe <watanabe.yu+github@gmail.com>2024-10-18 07:22:58 +0200
commitfafc3c2d5c7fae6bad0f6dc51611ae9390589ade (patch)
tree9d582f98ad89741324109494bf37a93550182fad /src/basic/alloc-util.h
parentMerge pull request #34801 from poettering/async-sd-notify-close (diff)
downloadsystemd-fafc3c2d5c7fae6bad0f6dc51611ae9390589ade.tar.xz
systemd-fafc3c2d5c7fae6bad0f6dc51611ae9390589ade.zip
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
Diffstat (limited to '')
-rw-r--r--src/basic/alloc-util.h5
1 files changed, 4 insertions, 1 deletions
diff --git a/src/basic/alloc-util.h b/src/basic/alloc-util.h
index 462092703a..ba71298287 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) \
({ \