From: Michael Paquier Date: Thu, 18 Dec 2025 02:01:43 +0000 (+0900) Subject: btree_gist: Fix memory allocation formula X-Git-Url: http://git.postgresql.org/gitweb/static//%22groups.php?a=commitdiff_plain;p=postgresql.git btree_gist: Fix memory allocation formula This change has been suggested by the two authors listed in this commit, both of them providing an incomplete solution (David's formula relied on a "bytea *", while Bertrand's did not use palloc_array()). The solution provided in this commit uses GBT_VARKEY instead of the inconsistent bytea for the allocation size, with a palloc_array(). The change related to Vsrt is one I am flipping to a more consistent style, in passing. Author: David Geier Author: Bertrand Drouvot Discussion: https://postgr.es/m/ad0748d4-3080-436e-b0bc-ac8f86a3466a@gmail.com Discussion: https://postgr.es/m/aTrG3Fi4APtfiCvQ@ip-10-97-1-34.eu-west-3.compute.internal --- diff --git a/contrib/btree_gist/btree_utils_var.c b/contrib/btree_gist/btree_utils_var.c index 7fbea0cfb7b..40e06ae4908 100644 --- a/contrib/btree_gist/btree_utils_var.c +++ b/contrib/btree_gist/btree_utils_var.c @@ -467,7 +467,7 @@ gbt_var_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, GBT_VARKEY **sv = NULL; gbt_vsrt_arg varg; - arr = (Vsrt *) palloc((maxoff + 1) * sizeof(Vsrt)); + arr = palloc_array(Vsrt, maxoff + 1); nbytes = (maxoff + 2) * sizeof(OffsetNumber); v->spl_left = (OffsetNumber *) palloc(nbytes); v->spl_right = (OffsetNumber *) palloc(nbytes); @@ -476,7 +476,7 @@ gbt_var_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, v->spl_nleft = 0; v->spl_nright = 0; - sv = palloc(sizeof(bytea *) * (maxoff + 1)); + sv = palloc_array(GBT_VARKEY *, maxoff + 1); /* Sort entries */