Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cba393a

Browse files
committed
In the Snowball dictionary, don't try to stem excessively-long words.
If the input word exceeds 1000 bytes, don't pass it to the stemmer; just return it as-is after case folding. Such an input is surely not a word in any human language, so whatever the stemmer might do to it would be pretty dubious in the first place. Adding this restriction protects us against a known recursion-to-stack-overflow problem in the Turkish stemmer, and it seems like good insurance against any other safety or performance issues that may exist in the Snowball stemmers. (I note, for example, that they contain no CHECK_FOR_INTERRUPTS calls, so we really don't want them running for a long time.) The threshold of 1000 bytes is arbitrary. An alternative definition could have been to treat such words as stopwords, but that seems like a bigger break from the old behavior. Per report from Egor Chindyaskin and Alexander Lakhin. Thanks to Olly Betts for the recommendation to fix it this way. Discussion: https://postgr.es/m/[email protected]
1 parent 5bed28e commit cba393a

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/backend/snowball/dict_snowball.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,24 @@ dsnowball_lexize(PG_FUNCTION_ARGS)
237237
char *txt = lowerstr_with_len(in, len);
238238
TSLexeme *res = palloc0(sizeof(TSLexeme) * 2);
239239

240-
if (*txt == '\0' || searchstoplist(&(d->stoplist), txt))
240+
/*
241+
* Do not pass strings exceeding 1000 bytes to the stemmer, as they're
242+
* surely not words in any human language. This restriction avoids
243+
* wasting cycles on stuff like base64-encoded data, and it protects us
244+
* against possible inefficiency or misbehavior in the stemmer. (For
245+
* example, the Turkish stemmer has an indefinite recursion, so it can
246+
* crash on long-enough strings.) However, Snowball dictionaries are
247+
* defined to recognize all strings, so we can't reject the string as an
248+
* unknown word.
249+
*/
250+
if (len > 1000)
251+
{
252+
/* return the lexeme lowercased, but otherwise unmodified */
253+
res->lexeme = txt;
254+
}
255+
else if (*txt == '\0' || searchstoplist(&(d->stoplist), txt))
241256
{
257+
/* empty or stopword, so report as stopword */
242258
pfree(txt);
243259
}
244260
else

0 commit comments

Comments
 (0)