postgresql/src/backend/tsearch
Tom Lane bc428b12ac 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 5a617d75d, 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
2023-04-06 15:52:37 -04:00
..
dicts Improve regression test coverage for src/backend/tsearch/spell.c. 2018-04-13 13:49:52 -04:00
dict.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
dict_ispell.c Avoid unnecessary use of pg_strcasecmp for already-downcased identifiers. 2018-01-26 18:25:14 -05:00
dict_simple.c Avoid unnecessary use of pg_strcasecmp for already-downcased identifiers. 2018-01-26 18:25:14 -05:00
dict_synonym.c Avoid unnecessary use of pg_strcasecmp for already-downcased identifiers. 2018-01-26 18:25:14 -05:00
dict_thesaurus.c Avoid unnecessary use of pg_strcasecmp for already-downcased identifiers. 2018-01-26 18:25:14 -05:00
Makefile Update copyright for 2018 2018-01-02 23:30:12 -05:00
regis.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
spell.c Defend against stack overrun in a few more places. 2022-08-24 13:01:40 -04:00
to_tsany.c Post-feature-freeze pgindent run. 2018-04-26 14:47:16 -04:00
ts_locale.c Avoid possible dangling-pointer access in tsearch_readline_callback. 2020-09-23 11:36:13 -04:00
ts_parse.c Add comments and a missing CHECK_FOR_INTERRUPTS in ts_headline. 2022-11-21 17:07:07 -05:00
ts_selfuncs.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
ts_typanalyze.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
ts_utils.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
wparser.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
wparser_def.c Fix ts_headline() edge cases for empty query and empty search text. 2023-04-06 15:52:37 -04:00