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

From: Anthonin Bonnefoy <anthonin(dot)bonnefoy(at)datadoghq(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Thomas Munro <thomas(dot)munro(at)gmail(dot)com>, Andres Freund <andres(at)anarazel(dot)de>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Converting tab-complete.c's else-if chain to a switch
Date: 2024-10-10 07:22:12
Message-ID: CAO6_XqrSRE7c_i+D7Hm07K3+6S0jTAmMr60RY41XzaA29Ae5uA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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.

Attachment Content-Type Size
v1-0001-Fix-psql-segfault-on-word-completion-without-matc.patch application/octet-stream 1.7 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Daniel Gustafsson 2024-10-10 07:26:15 Re: Doc: typo in config.sgml
Previous Message Masahiko Sawada 2024-10-10 07:05:10 Re: Add contrib/pg_logicalsnapinspect