Skip to content

Commit 34ad3ae

Browse files
committed
Fix ts_headline() edge cases for empty query and empty search text.
tsquery's GETQUERY() macro is only safe to apply to a tsquery that is known non-empty; otherwise it gives a pointer to garbage. Before commit 5a617d7, ts_headline() avoided this pitfall, but only in a very indirect, nonobvious way. (hlCover could not reach its TS_execute call, because if the query contains no lexemes then hlFirstIndex would surely return -1.) After that commit, it fell into the trap, resulting in weird errors such as "unrecognized operator" and/or valgrind complaints. In HEAD, fix this by not calling TS_execute_locations() at all for an empty query. In the back branches, add a defensive check to hlCover() --- that's not fixing any live bug, but I judge the code a bit too fragile as-is. Also, both mark_hl_fragments() and mark_hl_words() were careless about the possibility of empty search text: in the cases where no match has been found, they'd end up telling mark_fragment() to mark from word indexes 0 to 0 inclusive, even when there is no word 0. This is harmless since we over-allocated the prs->words array, but it does annoy valgrind. Fix so that the end index is -1 and thus mark_fragment() will do nothing in such cases. Bottom line is that this fixes a live bug in HEAD, but in the back branches it's only getting rid of a valgrind nitpick. Back-patch anyway. Per report from Alexander Lakhin. Discussion: https://postgr.es/m/c27f642d-020b-01ff-ae61-086af287c4fd@gmail.com
1 parent 0a6aaf0 commit 34ad3ae

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/backend/tsearch/wparser_def.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,6 +2039,9 @@ hlCover(HeadlineParsedText *prs, TSQuery query, int max_cover,
20392039
nextpmax;
20402040
hlCheck ch;
20412041

2042+
if (query->size <= 0)
2043+
return false; /* empty query matches nothing */
2044+
20422045
/*
20432046
* We look for the earliest, shortest substring of prs->words that
20442047
* satisfies the query. Both the pmin and pmax indices must be words
@@ -2343,7 +2346,8 @@ mark_hl_fragments(HeadlineParsedText *prs, TSQuery query, bool highlightall,
23432346
/* show the first min_words words if we have not marked anything */
23442347
if (num_f <= 0)
23452348
{
2346-
startpos = endpos = curlen = 0;
2349+
startpos = curlen = 0;
2350+
endpos = -1;
23472351
for (i = 0; i < prs->curwords && curlen < min_words; i++)
23482352
{
23492353
if (!NONWORDTOKEN(prs->words[i].type))
@@ -2498,7 +2502,7 @@ mark_hl_words(HeadlineParsedText *prs, TSQuery query, bool highlightall,
24982502
if (bestlen < 0)
24992503
{
25002504
curlen = 0;
2501-
pose = 0;
2505+
pose = -1;
25022506
for (i = 0; i < prs->curwords && curlen < min_words; i++)
25032507
{
25042508
if (!NONWORDTOKEN(prs->words[i].type))

src/test/regress/expected/tsearch.out

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,6 +1990,27 @@ to_tsquery('english','Lorem') && phraseto_tsquery('english','ullamcorper urna'),
19901990
<b>Lorem</b> ipsum <b>urna</b>. Nullam nullam <b>ullamcorper</b> <b>urna</b>
19911991
(1 row)
19921992

1993+
-- Edge cases with empty query
1994+
SELECT ts_headline('english',
1995+
'', ''::tsquery);
1996+
NOTICE: text-search query doesn't contain lexemes: ""
1997+
LINE 2: '', ''::tsquery);
1998+
^
1999+
ts_headline
2000+
-------------
2001+
2002+
(1 row)
2003+
2004+
SELECT ts_headline('english',
2005+
'foo bar', ''::tsquery);
2006+
NOTICE: text-search query doesn't contain lexemes: ""
2007+
LINE 2: 'foo bar', ''::tsquery);
2008+
^
2009+
ts_headline
2010+
-------------
2011+
foo bar
2012+
(1 row)
2013+
19932014
--Rewrite sub system
19942015
CREATE TABLE test_tsquery (txtkeyword TEXT, txtsample TEXT);
19952016
\set ECHO none

src/test/regress/sql/tsearch.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,12 @@ SELECT ts_headline('english',
549549
to_tsquery('english','Lorem') && phraseto_tsquery('english','ullamcorper urna'),
550550
'MaxFragments=100, MaxWords=100, MinWords=1');
551551

552+
-- Edge cases with empty query
553+
SELECT ts_headline('english',
554+
'', ''::tsquery);
555+
SELECT ts_headline('english',
556+
'foo bar', ''::tsquery);
557+
552558
--Rewrite sub system
553559

554560
CREATE TABLE test_tsquery (txtkeyword TEXT, txtsample TEXT);

0 commit comments

Comments
 (0)