Re: Converting tab-complete.c's else-if chain to a switch

Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>

From: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Thomas Munro <thomas.munro@gmail.com>, Andres Freund <andres@anarazel.de>, pgsql-hackers@lists.postgresql.org
Date: 2024-10-10T07:22:12Z
Lists: pgsql-hackers

Attachments

Hi,

bd1276a3c9 seems to have introduced a segfault when trying to complete
a word that doesn't have any match. For example, 'postgres=# z\t' will
yield the following backtrace:

#0: psql`pg_strcasecmp(s1="", s2="ACCESS METHOD") at pgstrcasecmp.c:40:39
#1: psql`psql_completion(text=":pgss-", start=0, end=6) at
tab-complete.in.c:2033:8

The matching code in tab-complete.in.c:
if (matches == NULL)
{
    const pgsql_thing_t *wac;
    for (wac = words_after_create; wac->name != NULL; wac++)
    {
        if (pg_strcasecmp(prev_wd, wac->name) == 0)
        {

psql tries to read the previous word when matches are NULL. However,
there's no previous word set here, leading to the segfault.

I've attached a patch that checks if the previous word does exist
before trying to use it, along with an additional test for this
codepath.

Commits

  1. Don't hard-code the input file name in gen_tabcomplete.pl's output.

  2. Avoid possible segfault in psql's tab completion.

  3. Convert tab-complete's long else-if chain to a switch statement.

  4. Prepare tab-complete.c for preprocessing.

  5. Invent "MatchAnyN" option for tab-complete.c's Matches/MatchesCS.