From 40c426c7094fcfebe60e4f4ac22c7fd1998fd210 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Fri, 20 Nov 2026 15:25:10 +0100 Subject: [PATCH v2 6/7] WIP: flex_size FIXME: consider multiplication overflow checks --- contrib/pg_trgm/trgm.h | 5 ++--- contrib/pg_trgm/trgm_gist.c | 3 +-- contrib/pg_trgm/trgm_op.c | 3 +-- src/backend/utils/adt/numeric.c | 6 +----- src/include/c.h | 18 ++++++++++++++++++ src/include/common/fe_memutils.h | 9 +++++++++ src/include/utils/palloc.h | 7 +++++++ 7 files changed, 39 insertions(+), 12 deletions(-) diff --git a/contrib/pg_trgm/trgm.h b/contrib/pg_trgm/trgm.h index 7728aea84d0..48c954f5b81 100644 --- a/contrib/pg_trgm/trgm.h +++ b/contrib/pg_trgm/trgm.h @@ -56,9 +56,8 @@ extern int (*CMPTRGM) (const void *a, const void *b); * meta-character */ /* - * Note: This structure has trailing padding. When allocating a header-only - * value, at least sizeof(TRGM) must be allocated, to avoid -Warray-bounds - * compiler warnings. + * Note: This structure has trailing padding, so palloc_flex_array() should + * be used. */ typedef struct { diff --git a/contrib/pg_trgm/trgm_gist.c b/contrib/pg_trgm/trgm_gist.c index 85c7da37125..68075010e8f 100644 --- a/contrib/pg_trgm/trgm_gist.c +++ b/contrib/pg_trgm/trgm_gist.c @@ -80,8 +80,7 @@ gtrgm_alloc(bool isalltrue, int siglen, BITVECP sign) int size = CALCGTSIZE(flag, siglen); TRGM *res; - /* allocate at least sizeof(TRGM), to cover padding */ - res = palloc(Max(size, sizeof(TRGM))); + res = palloc_flex_array(TRGM, data, size - TRGMHDRSIZE); SET_VARSIZE(res, size); res->flag = flag; diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c index 2655257c2ad..01699dc1d46 100644 --- a/contrib/pg_trgm/trgm_op.c +++ b/contrib/pg_trgm/trgm_op.c @@ -1097,8 +1097,7 @@ generate_wildcard_trgm(const char *str, int slen) if (slen + LPADDING + RPADDING < 3 || slen == 0) { - /* allocate at least sizeof(TRGM), to cover padding */ - trg = (TRGM *) palloc(Max(TRGMHDRSIZE, sizeof(TRGM))); + trg = palloc_flex_array(TRGM, data, 0); trg->flag = ARRKEY; SET_VARSIZE(trg, TRGMHDRSIZE); return trg; diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index 18a7877f194..5f8583a7b91 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -7636,11 +7636,7 @@ make_result_safe(const NumericVar *var, Node *escontext) sign == NUMERIC_NINF)) elog(ERROR, "invalid numeric sign value 0x%x", sign); - /* - * We must allocate at least the full struct size, even though the - * datum only uses NUMERIC_HDRSZ_SHORT bytes. - */ - result = (Numeric) palloc(sizeof(struct NumericData)); + result = palloc_flex_array(struct NumericData, choice.n_short.n_data, 0); SET_VARSIZE(result, NUMERIC_HDRSZ_SHORT); result->choice.n_header = sign; diff --git a/src/include/c.h b/src/include/c.h index 0b9afcd7d3c..5f66e9e3b1d 100644 --- a/src/include/c.h +++ b/src/include/c.h @@ -1226,6 +1226,24 @@ pg_noreturn extern void ExceptionalCondition(const char *conditionName, */ #define Min(x, y) ((x) < (y) ? (x) : (y)) +/* + * flex_size + * Return the size of a struct "type" whose flexible array member + * "member" holds "count" elements. + * + * The result is never less than sizeof(type), so that any trailing padding + * of the struct is covered. "count" is evaluated only once. + */ +#define flex_size(type, member, count) \ + flex_size_impl(sizeof(type), offsetof(type, member), \ + sizeof(((type *) 0)->member[0]), (count)) + +static inline size_t +flex_size_impl(size_t structsz, size_t offset, size_t elemsz, size_t count) +{ + return Max(structsz, offset + count * elemsz); +} + /* Get a bit mask of the bits set in non-size_t aligned addresses */ #define SIZE_T_ALIGN_MASK (sizeof(size_t) - 1) diff --git a/src/include/common/fe_memutils.h b/src/include/common/fe_memutils.h index e701e38bec5..5e45a44ba4c 100644 --- a/src/include/common/fe_memutils.h +++ b/src/include/common/fe_memutils.h @@ -61,6 +61,13 @@ pg_attribute_alloc_size(2, 3) pg_attribute_returns_nonnull extern void *pg_reall #define pg_malloc_object(type) ((type *) pg_malloc(sizeof(type))) #define pg_malloc0_object(type) ((type *) pg_malloc0(sizeof(type))) +/* + * Allocate space for a struct of type "type" whose flexible array member + * "member" holds "count" elements. See flex_size() for details. + */ +#define pg_malloc_flex_array(type, member, count) ((type *) pg_malloc(flex_size(type, member, count))) +#define pg_malloc0_flex_array(type, member, count) ((type *) pg_malloc0(flex_size(type, member, count))) + /* * Allocate space for "count" objects of type "type" */ @@ -91,6 +98,8 @@ pg_attribute_alloc_size(2, 3) pg_attribute_returns_nonnull extern void *repalloc #define palloc0_object(type) ((type *) palloc0(sizeof(type))) #define palloc_array(type, count) ((type *) palloc_mul(sizeof(type), count)) #define palloc0_array(type, count) ((type *) palloc0_mul(sizeof(type), count)) +#define palloc_flex_array(type, member, count) ((type *) palloc(flex_size(type, member, count))) +#define palloc0_flex_array(type, member, count) ((type *) palloc0(flex_size(type, member, count))) #define palloc_array_extended(type, count, flags) ((type *) palloc_mul_extended(sizeof(type), count, flags)) #define repalloc_array(pointer, type, count) ((type *) repalloc_mul(pointer, sizeof(type), count)) diff --git a/src/include/utils/palloc.h b/src/include/utils/palloc.h index e8a8eefd9c4..265a3f1a727 100644 --- a/src/include/utils/palloc.h +++ b/src/include/utils/palloc.h @@ -107,6 +107,13 @@ pg_nodiscard pg_attribute_alloc_size(2, 3) extern void *repalloc_mul_extended(vo #define palloc_object(type) ((type *) palloc(sizeof(type))) #define palloc0_object(type) ((type *) palloc0(sizeof(type))) +/* + * Allocate space for a struct of type "type" whose flexible array member + * "member" holds "count" elements. See flex_size() for details. + */ +#define palloc_flex_array(type, member, count) ((type *) palloc(flex_size(type, member, count))) +#define palloc0_flex_array(type, member, count) ((type *) palloc0(flex_size(type, member, count))) + /* * Allocate space for "count" objects of type "type" */ -- 2.56.0