pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Paquier <michael@paquier.xyz>
To: hghwng@gmail.com
To: pgsql-bugs@lists.postgresql.org
Cc: Alvaro Herrera <alvherre@2ndquadrant.com>
Subject: Re: BUG #16330: psql accesses null pointer in connect.c:do_connect
Date: Mon, 30 Mar 2020 16:19:37 +0900
Message-ID: <20200330071937.GC43995@paquier.xyz> (raw)
In-Reply-To: <16330-b34835d83619e25d@postgresql.org>
References: <16330-b34835d83619e25d@postgresql.org>

On Mon, Mar 30, 2020 at 02:20:48AM +0000, PG Bug reporting form wrote:
> If the connection to postmaster is closed, then trying to re-connect to
> another one leads to SIGSEGV.
> 
> REPRODUCE:
> $ psql
> -> \conninfo
> You are connected to database "hugh" as user "hugh" via socket in
> "/run/postgresql" at port "5432".
> *shut down server with commands like "systemctl stop postgresql"*
> -> \conninfo 
> You are currently not connected to a database.
> -> \c a b c d
> [1]    984978 segmentation fault (core dumped)  psql

Indeed.  Reproduced the problem here on HEAD and REL_12_STABLE, though
you need to execute a command after stopping the server to let psql
know that you are not connected to a database.

> ANALYSIS:
> PQhost(o_conn) returns NULL, and strcmp(host, NULL) raises SIGSEGV.

Yes, the problem comes from this commit (added Alvaro in CC):
commit: 6e5f8d489acccdc50a35a1b7db8e72b5ad579253
author: Alvaro Herrera <alvherre@alvh.no-ip.org>
date: Mon, 19 Nov 2018 14:34:12 -0300
psql: Show IP address in \conninfo

And more particularly this bit that ignores the possibility of
PQhost() being NULL:
-   if (!user && reuse_previous)
-       user = PQuser(o_conn);
-   if (!host && reuse_previous)
-       host = PQhost(o_conn);
-   if (!port && reuse_previous)
-       port = PQport(o_conn);
+   if (reuse_previous)
+   {
+       if (!user)
+           user = PQuser(o_conn);
+       if (host && strcmp(host, PQhost(o_conn)) == 0)
+       {
+           /*
+            * if we are targetting the same host, reuse its hostaddr for
+            * consistency
+            */
+           hostaddr = PQhostaddr(o_conn);

A fix like the attached should be sufficient as if we know that
PQhost() is NULL for the old connection we cannot use the old
hostaddr.  Alvaro, what do you think?
--
Michael

Attachments:

psql-crash-fix.patch (text/x-diff, 468B) download diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index abb18a19c2..8108a76a77 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -2990,7 +2990,8 @@ do_connect(enum trivalue reuse_previous_specification, { if (!user) user = PQuser(o_conn); - if (host && strcmp(host, PQhost(o_conn)) == 0 && + if (host && PQhost(o_conn) && + strcmp(host, PQhost(o_conn)) == 0 && has_hostaddr(o_conn)) { hostaddr = PQhostaddr(o_conn);
signature.asc (application/pgp-signature, 832B) download

view thread (8+ messages)  latest in thread

Message-ID: <20200330071937.GC43995@paquier.xyz>
Permalink:  ../20200330071937.GC43995@paquier.xyz/
Also on:    postgresql.org/message-id/20200330071937.GC43995@paquier.xyz

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: michael@paquier.xyz, hghwng@gmail.com, alvherre@2ndquadrant.com
  Subject: Re: BUG #16330: psql accesses null pointer in connect.c:do_connect
  In-Reply-To: <20200330071937.GC43995@paquier.xyz>
  References: <16330-b34835d83619e25d@postgresql.org> <20200330071937.GC43995@paquier.xyz>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox