From c1607b0a0724f8970c72fabe349773d9ab737a7f Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Fri, 13 Nov 2026 11:58:18 +0100 Subject: [PATCH 3/5] Fixes for alloc_size attribute [pg_trgm] Header-only pg_trgm values are now allocated with at least the size of the struct, to avoid -Warray-bounds warnings. --- contrib/pg_trgm/trgm.h | 5 +++++ contrib/pg_trgm/trgm_gist.c | 5 ++++- contrib/pg_trgm/trgm_op.c | 3 ++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/contrib/pg_trgm/trgm.h b/contrib/pg_trgm/trgm.h index ca23aad4dd9..7728aea84d0 100644 --- a/contrib/pg_trgm/trgm.h +++ b/contrib/pg_trgm/trgm.h @@ -55,6 +55,11 @@ extern int (*CMPTRGM) (const void *a, const void *b); #define ISWILDCARDCHAR(x) (*(x) == '_' || *(x) == '%') /* Wildcard * 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. + */ typedef struct { int32 vl_len_; /* varlena header (do not touch directly!) */ diff --git a/contrib/pg_trgm/trgm_gist.c b/contrib/pg_trgm/trgm_gist.c index dfa2f968d77..85c7da37125 100644 --- a/contrib/pg_trgm/trgm_gist.c +++ b/contrib/pg_trgm/trgm_gist.c @@ -78,7 +78,10 @@ gtrgm_alloc(bool isalltrue, int siglen, BITVECP sign) { int flag = SIGNKEY | (isalltrue ? ALLISTRUE : 0); int size = CALCGTSIZE(flag, siglen); - TRGM *res = palloc(size); + TRGM *res; + + /* allocate at least sizeof(TRGM), to cover padding */ + res = palloc(Max(size, sizeof(TRGM))); SET_VARSIZE(res, size); res->flag = flag; diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c index 22bcc3c3361..2655257c2ad 100644 --- a/contrib/pg_trgm/trgm_op.c +++ b/contrib/pg_trgm/trgm_op.c @@ -1097,7 +1097,8 @@ generate_wildcard_trgm(const char *str, int slen) if (slen + LPADDING + RPADDING < 3 || slen == 0) { - trg = (TRGM *) palloc(TRGMHDRSIZE); + /* allocate at least sizeof(TRGM), to cover padding */ + trg = (TRGM *) palloc(Max(TRGMHDRSIZE, sizeof(TRGM))); trg->flag = ARRKEY; SET_VARSIZE(trg, TRGMHDRSIZE); return trg; -- 2.56.0