summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Gierth <rhodiumtoad@postgresql.org>2020-11-12 14:34:37 +0000
committerAndrew Gierth <rhodiumtoad@postgresql.org>2020-11-12 14:55:51 +0000
commit4b212b7c87754095d4f89b03a3aaec6e32d09342 (patch)
tree22eb15cce60137432b837f71b9419153424bf3d7
parent210564a74409f79cb1441fab62c4154dd148840f (diff)
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
-rw-r--r--contrib/pg_trgm/trgm_gist.c10
1 files changed, 4 insertions, 6 deletions
diff --git a/contrib/pg_trgm/trgm_gist.c b/contrib/pg_trgm/trgm_gist.c
index 07d1dc308bb..1e0c19a5e8b 100644
--- a/contrib/pg_trgm/trgm_gist.c
+++ b/contrib/pg_trgm/trgm_gist.c
@@ -759,7 +759,7 @@ Datum
gtrgm_picksplit(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
- OffsetNumber maxoff = entryvec->n - 2;
+ OffsetNumber maxoff = entryvec->n - 1;
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
OffsetNumber k,
j;
@@ -782,7 +782,7 @@ gtrgm_picksplit(PG_FUNCTION_ARGS)
SPLITCOST *costvector;
/* cache the sign data for each existing item */
- cache = (CACHESIGN *) palloc(sizeof(CACHESIGN) * (maxoff + 2));
+ cache = (CACHESIGN *) palloc(sizeof(CACHESIGN) * (maxoff + 1));
for (k = FirstOffsetNumber; k <= maxoff; k = OffsetNumberNext(k))
fillcache(&cache[k], GETENTRY(entryvec, k));
@@ -809,7 +809,7 @@ gtrgm_picksplit(PG_FUNCTION_ARGS)
}
/* initialize the result vectors */
- nbytes = (maxoff + 2) * sizeof(OffsetNumber);
+ nbytes = maxoff * sizeof(OffsetNumber);
v->spl_left = left = (OffsetNumber *) palloc(nbytes);
v->spl_right = right = (OffsetNumber *) palloc(nbytes);
v->spl_nleft = 0;
@@ -845,8 +845,7 @@ gtrgm_picksplit(PG_FUNCTION_ARGS)
union_l = GETSIGN(datum_l);
union_r = GETSIGN(datum_r);
- maxoff = OffsetNumberNext(maxoff);
- fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff));
+
/* sort before ... */
costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
@@ -932,7 +931,6 @@ gtrgm_picksplit(PG_FUNCTION_ARGS)
}
}
- *right = *left = FirstOffsetNumber;
v->spl_ldatum = PointerGetDatum(datum_l);
v->spl_rdatum = PointerGetDatum(datum_r);