Skip to content

Commit 3836d4b

Browse files
committed
pg_trgm: fix crash in 2-item picksplit
Whether from size overflow in gistSplit or from secondary splits, picksplit is (rarely) called with exactly two items to split. Formerly, due to special-case handling of the last item, this would lead to access to an uninitialized cache entry; prior to PG 13 this might have been harmless or at worst led to an incorrect union datum, but in 13 onwards it can cause a backend crash from using an uninitialized pointer. Repair by removing the special case, which was deemed not to have been appropriate anyway. Backpatch all the way, because this bug has existed since pg_trgm was added. Per report on IRC from user "ftzdomino". Analysis and testing by me, patch from Alexander Korotkov. Discussion: https://postgr.es/m/87k0usfdxg.fsf@news-spur.riddles.org.uk
1 parent 9c4f519 commit 3836d4b

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

contrib/pg_trgm/trgm_gist.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ Datum
786786
gtrgm_picksplit(PG_FUNCTION_ARGS)
787787
{
788788
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
789-
OffsetNumber maxoff = entryvec->n - 2;
789+
OffsetNumber maxoff = entryvec->n - 1;
790790
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
791791
int siglen = GET_SIGLEN();
792792
OffsetNumber k,
@@ -811,8 +811,8 @@ gtrgm_picksplit(PG_FUNCTION_ARGS)
811811
SPLITCOST *costvector;
812812

813813
/* cache the sign data for each existing item */
814-
cache = (CACHESIGN *) palloc(sizeof(CACHESIGN) * (maxoff + 2));
815-
cache_sign = palloc(siglen * (maxoff + 2));
814+
cache = (CACHESIGN *) palloc(sizeof(CACHESIGN) * (maxoff + 1));
815+
cache_sign = palloc(siglen * (maxoff + 1));
816816

817817
for (k = FirstOffsetNumber; k <= maxoff; k = OffsetNumberNext(k))
818818
fillcache(&cache[k], GETENTRY(entryvec, k), &cache_sign[siglen * k],
@@ -841,7 +841,7 @@ gtrgm_picksplit(PG_FUNCTION_ARGS)
841841
}
842842

843843
/* initialize the result vectors */
844-
nbytes = (maxoff + 2) * sizeof(OffsetNumber);
844+
nbytes = maxoff * sizeof(OffsetNumber);
845845
v->spl_left = left = (OffsetNumber *) palloc(nbytes);
846846
v->spl_right = right = (OffsetNumber *) palloc(nbytes);
847847
v->spl_nleft = 0;
@@ -853,9 +853,6 @@ gtrgm_picksplit(PG_FUNCTION_ARGS)
853853

854854
union_l = GETSIGN(datum_l);
855855
union_r = GETSIGN(datum_r);
856-
maxoff = OffsetNumberNext(maxoff);
857-
fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff),
858-
&cache_sign[siglen * maxoff], siglen);
859856

860857
/* sort before ... */
861858
costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
@@ -944,7 +941,6 @@ gtrgm_picksplit(PG_FUNCTION_ARGS)
944941
}
945942
}
946943

947-
*right = *left = FirstOffsetNumber;
948944
v->spl_ldatum = PointerGetDatum(datum_l);
949945
v->spl_rdatum = PointerGetDatum(datum_r);
950946

0 commit comments

Comments
 (0)