From 71bb73f60be71085a8f3094a64d0883cef3c0afc Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Mon, 25 Sep 2023 11:50:28 -0400 Subject: [PATCH] Limit to_tsvector_byid's initial array allocation to something sane. The initial estimate of the number of distinct ParsedWords is just that: an estimate. Don't let it exceed what palloc is willing to allocate. If in fact we need more entries, we'll eventually fail trying to enlarge the array. But if we don't, this allows success on inputs that currently draw "invalid memory alloc request size". Per bug #18080 from Uwe Binder. Back-patch to all supported branches. Discussion: https://postgr.es/m/18080-d5c5e58fef8c99b7@postgresql.org --- src/backend/tsearch/to_tsany.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/backend/tsearch/to_tsany.c b/src/backend/tsearch/to_tsany.c index edeffacc2d7..361aa997daa 100644 --- a/src/backend/tsearch/to_tsany.c +++ b/src/backend/tsearch/to_tsany.c @@ -252,6 +252,8 @@ to_tsvector_byid(PG_FUNCTION_ARGS) * number */ if (prs.lenwords < 2) prs.lenwords = 2; + else if (prs.lenwords > MaxAllocSize / sizeof(ParsedWord)) + prs.lenwords = MaxAllocSize / sizeof(ParsedWord); prs.curwords = 0; prs.pos = 0; prs.words = (ParsedWord *) palloc(sizeof(ParsedWord) * prs.lenwords);