From dc9a2d54fd2571c21c074103cc8ddd93e840b985 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Herrera?= Date: Sun, 11 May 2025 09:22:12 -0400 Subject: relcache: Avoid memory leak on tables with no CHECK constraints As complained about by Valgrind, in commit a379061a22a8 I failed to realize that I was causing rd_att->constr->check to become allocated when no CHECK constraints exist; previously it'd remain NULL. (This was my bug, not the mentioned commit author's). Fix by making the allocation conditional, and set ->check to NULL if unallocated. Reported-by: Yasir Reviewed-by: Tom Lane Discussion: https://postgr.es/m/202505082025.57ijx3qrbx7u@alvherre.pgsql --- src/backend/utils/cache/relcache.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c index 68ff67de549..559ba9cdb2c 100644 --- a/src/backend/utils/cache/relcache.c +++ b/src/backend/utils/cache/relcache.c @@ -4598,10 +4598,13 @@ CheckNNConstraintFetch(Relation relation) HeapTuple htup; int found = 0; - /* Allocate array with room for as many entries as expected */ - check = (ConstrCheck *) - MemoryContextAllocZero(CacheMemoryContext, - ncheck * sizeof(ConstrCheck)); + /* Allocate array with room for as many entries as expected, if needed */ + if (ncheck > 0) + check = (ConstrCheck *) + MemoryContextAllocZero(CacheMemoryContext, + ncheck * sizeof(ConstrCheck)); + else + check = NULL; /* Search pg_constraint for relevant entries */ ScanKeyInit(&skey[0], -- cgit v1.2.3