postgres.git / summary / log / commit / refs
commit ef9063a8870fb5fb1666da3399ff666201eccf70
Author: Tom Lane <tgl@sss.pgh.pa.us>
Date: Thu Aug 13 15:20:27 2026 +0000
Consistently enforce tsvector/tsquery maximum lengths.
Some places rejected individual tokens longer than MAXSTRLEN, while
others rejected ones longer than MAXSTRLEN-1. The data structure is
perfectly capable of handling MAXSTRLEN, so there's nothing wrong
with using the looser bound. Moreover, as things stand there is a
dump/reload hazard: some code paths permit construction of a tsvector
or tsquery that would later be rejected by tsvectorin or tsqueryin.
So standardize on using MAXSTRLEN.
Identical remarks apply to MAXSTRPOS (the total data length),
so fix that too.
Back-patch, in hopes of avoiding cases where a value acceptable to
one supported release is not acceptable to another.
Author: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Zsolt Parragi <zsolt.parragi@percona.com>
Discussion: https://postgr.es/m/CAN4CZFNYQo4zfbRR435uD0vSfuy5y7dnFOXDfKr9zYoL1JnAxA@mail.gmail.com
Backpatch-through: 14
src/backend/tsearch/ts_parse.c | 4 ++--
src/backend/utils/adt/tsquery.c | 6 +++---
src/backend/utils/adt/tsvector.c | 4 ++--
src/backend/utils/adt/tsvector_op.c | 4 ++--
4 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/src/backend/tsearch/ts_parse.c b/src/backend/tsearch/ts_parse.c
index be7a28b15c5..cef31c540ba 100644
--- a/src/backend/tsearch/ts_parse.c
+++ b/src/backend/tsearch/ts_parse.c
@@ -378,7 +378,7 @@ parsetext(Oid cfgId, ParsedText *prs, char *buf, int buflen)
PointerGetDatum(&lemm),
PointerGetDatum(&lenlemm)));
- if (type > 0 && lenlemm >= MAXSTRLEN)
+ if (type > 0 && lenlemm > MAXSTRLEN)
{
#ifdef IGNORE_LONGLEXEME
ereport(NOTICE,
@@ -581,7 +581,7 @@ hlparsetext(Oid cfgId, HeadlineParsedText *prs, TSQuery query, char *buf, int bu
PointerGetDatum(&lemm),
PointerGetDatum(&lenlemm)));
- if (type > 0 && lenlemm >= MAXSTRLEN)
+ if (type > 0 && lenlemm > MAXSTRLEN)
{
#ifdef IGNORE_LONGLEXEME
ereport(NOTICE,
diff --git a/src/backend/utils/adt/tsquery.c b/src/backend/utils/adt/tsquery.c
index ee86122bf80..68652a2d146 100644
--- a/src/backend/utils/adt/tsquery.c
+++ b/src/backend/utils/adt/tsquery.c
@@ -524,12 +524,12 @@ pushValue_internal(TSQueryParserState state, pg_crc32 valcrc, int distance, int
{
QueryOperand *tmp;
- if (distance >= MAXSTRPOS)
+ if (distance > MAXSTRPOS)
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("value is too big in tsquery: \"%s\"",
state->buffer)));
- if (lenval >= MAXSTRLEN)
+ if (lenval > MAXSTRLEN)
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("operand is too long in tsquery: \"%s\"",
@@ -557,7 +557,7 @@ pushValue(TSQueryParserState state, char *strval, int lenval, int16 weight, bool
{
pg_crc32 valcrc;
- if (lenval >= MAXSTRLEN)
+ if (lenval > MAXSTRLEN)
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("word is too long in tsquery: \"%s\"",
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index 082fab0d44a..3b17704de80 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -206,12 +206,12 @@ tsvectorin(PG_FUNCTION_ARGS)
while (gettoken_tsvector(state, &token, &toklen, &pos, &poslen, NULL))
{
- if (toklen >= MAXSTRLEN)
+ if (toklen > MAXSTRLEN)
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("word is too long (%ld bytes, max %ld bytes)",
(long) toklen,
- (long) (MAXSTRLEN - 1))));
+ (long) MAXSTRLEN)));
if (cur - tmpbuf > MAXSTRPOS)
ereport(ERROR,
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 66d909b994f..22add34f2bf 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -775,12 +775,12 @@ array_to_tsvector(PG_FUNCTION_ARGS)
ereport(ERROR,
(errcode(ERRCODE_ZERO_LENGTH_CHARACTER_STRING),
errmsg("lexeme array may not contain empty strings")));
- if (toklen >= MAXSTRLEN)
+ if (toklen > MAXSTRLEN)
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("word is too long (%d bytes, max %d bytes)",
toklen,
- MAXSTRLEN - 1)));
+ MAXSTRLEN)));
}
/* Sort and de-dup, because this is required for a valid tsvector. */
[parent: 1511993de69f]