pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #16330: psql accesses null pointer in connect.c:do_connect
8+ messages / 4 participants
[nested] [flat]
Commits from this thread:
2020-04-01 8d84dd0012 f79bea4b6b Fix crash in psql when attempting to reuse old connection
* BUG #16330: psql accesses null pointer in connect.c:do_connect
@ 2020-03-30 02:20 PG Bug reporting form <noreply@postgresql.org>
2020-03-30 07:19 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Michael Paquier <michael@paquier.xyz>
0 siblings, 1 reply; 8+ messages in thread
From: PG Bug reporting form @ 2020-03-30 02:20 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org; +Cc: hghwng@gmail.com
The following bug has been logged on the website:
Bug reference: 16330
Logged by: Hugh Wang
Email address: hghwng@gmail.com
PostgreSQL version: 12.2
Operating system: Arch Linux
Description:
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
ANALYSIS:
PQhost(o_conn) returns NULL, and strcmp(host, NULL) raises SIGSEGV.
SOURCE:
https://github.com/postgres/postgres/blob/master/src/bin/psql/command.c#L3016
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: BUG #16330: psql accesses null pointer in connect.c:do_connect
2020-03-30 02:20 BUG #16330: psql accesses null pointer in connect.c:do_connect PG Bug reporting form <noreply@postgresql.org>
@ 2020-03-30 07:19 ` Michael Paquier <michael@paquier.xyz>
2020-03-30 15:35 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 8+ messages in thread
From: Michael Paquier @ 2020-03-30 07:19 UTC (permalink / raw)
To: hghwng@gmail.com; pgsql-bugs@lists.postgresql.org; +Cc: Alvaro Herrera <alvherre@2ndquadrant.com>
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
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: BUG #16330: psql accesses null pointer in connect.c:do_connect
2020-03-30 02:20 BUG #16330: psql accesses null pointer in connect.c:do_connect PG Bug reporting form <noreply@postgresql.org>
2020-03-30 07:19 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Michael Paquier <michael@paquier.xyz>
@ 2020-03-30 15:35 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-03-30 15:48 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Alvaro Herrera <alvherre@2ndquadrant.com>
0 siblings, 1 reply; 8+ messages in thread
From: Tom Lane @ 2020-03-30 15:35 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: hghwng@gmail.com, pgsql-bugs@lists.postgresql.org, Alvaro Herrera <alvherre@2ndquadrant.com>
Michael Paquier <michael@paquier.xyz> writes:
> 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.
> 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?
It looks to me like there's a similar hazard a bit further down
(line 3029):
appendConnStrVal(&connstr, PQdb(o_conn));
I wonder if we should force reuse_previous to false if there's
no o_conn, rather than fixing this stuff piecemeal.
regards, tom lane
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: BUG #16330: psql accesses null pointer in connect.c:do_connect
2020-03-30 02:20 BUG #16330: psql accesses null pointer in connect.c:do_connect PG Bug reporting form <noreply@postgresql.org>
2020-03-30 07:19 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Michael Paquier <michael@paquier.xyz>
2020-03-30 15:35 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Tom Lane <tgl@sss.pgh.pa.us>
@ 2020-03-30 15:48 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2020-03-31 07:21 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Michael Paquier <michael@paquier.xyz>
0 siblings, 1 reply; 8+ messages in thread
From: Alvaro Herrera @ 2020-03-30 15:48 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Michael Paquier <michael@paquier.xyz>; hghwng@gmail.com; pgsql-bugs@lists.postgresql.org
On 2020-Mar-30, Tom Lane wrote:
> Michael Paquier <michael@paquier.xyz> writes:
> > 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.
>
> > 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?
>
> It looks to me like there's a similar hazard a bit further down
> (line 3029):
>
> appendConnStrVal(&connstr, PQdb(o_conn));
>
> I wonder if we should force reuse_previous to false if there's
> no o_conn, rather than fixing this stuff piecemeal.
That was my impression too.
--
Álvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: BUG #16330: psql accesses null pointer in connect.c:do_connect
2020-03-30 02:20 BUG #16330: psql accesses null pointer in connect.c:do_connect PG Bug reporting form <noreply@postgresql.org>
2020-03-30 07:19 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Michael Paquier <michael@paquier.xyz>
2020-03-30 15:35 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Tom Lane <tgl@sss.pgh.pa.us>
2020-03-30 15:48 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Alvaro Herrera <alvherre@2ndquadrant.com>
@ 2020-03-31 07:21 ` Michael Paquier <michael@paquier.xyz>
2020-03-31 13:43 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Tom Lane <tgl@sss.pgh.pa.us>
2020-03-31 13:52 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Alvaro Herrera <alvherre@2ndquadrant.com>
0 siblings, 2 replies; 8+ messages in thread
From: Michael Paquier @ 2020-03-31 07:21 UTC (permalink / raw)
To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; hghwng@gmail.com; pgsql-bugs@lists.postgresql.org
On Mon, Mar 30, 2020 at 12:48:51PM -0300, Alvaro Herrera wrote:
> On 2020-Mar-30, Tom Lane wrote:
>> It looks to me like there's a similar hazard a bit further down
>> (line 3029):
>>
>> appendConnStrVal(&connstr, PQdb(o_conn));
>>
>> I wonder if we should force reuse_previous to false if there's
>> no o_conn, rather than fixing this stuff piecemeal.
>
> That was my impression too.
Good point. What do you think about the attached then?
--
Michael
Attachments:
psql-crash-fix-2.patch (text/x-diff, 513B) download
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index abb18a19c2..db31fa8753 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -2974,6 +2974,11 @@ do_connect(enum trivalue reuse_previous_specification,
reuse_previous = !has_connection_string;
break;
}
+
+ /* If the old connection does not exist, there is nothing to reuse. */
+ if (!o_conn)
+ reuse_previous = false;
+
/* Silently ignore arguments subsequent to a connection string. */
if (has_connection_string)
{
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: BUG #16330: psql accesses null pointer in connect.c:do_connect
2020-03-30 02:20 BUG #16330: psql accesses null pointer in connect.c:do_connect PG Bug reporting form <noreply@postgresql.org>
2020-03-30 07:19 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Michael Paquier <michael@paquier.xyz>
2020-03-30 15:35 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Tom Lane <tgl@sss.pgh.pa.us>
2020-03-30 15:48 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Alvaro Herrera <alvherre@2ndquadrant.com>
2020-03-31 07:21 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Michael Paquier <michael@paquier.xyz>
@ 2020-03-31 13:43 ` Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 8+ messages in thread
From: Tom Lane @ 2020-03-31 13:43 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: Alvaro Herrera <alvherre@2ndquadrant.com>; hghwng@gmail.com; pgsql-bugs@lists.postgresql.org
Michael Paquier <michael@paquier.xyz> writes:
> On Mon, Mar 30, 2020 at 12:48:51PM -0300, Alvaro Herrera wrote:
>> On 2020-Mar-30, Tom Lane wrote:
>>> I wonder if we should force reuse_previous to false if there's
>>> no o_conn, rather than fixing this stuff piecemeal.
>> That was my impression too.
> Good point. What do you think about the attached then?
WFM.
regards, tom lane
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: BUG #16330: psql accesses null pointer in connect.c:do_connect
2020-03-30 02:20 BUG #16330: psql accesses null pointer in connect.c:do_connect PG Bug reporting form <noreply@postgresql.org>
2020-03-30 07:19 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Michael Paquier <michael@paquier.xyz>
2020-03-30 15:35 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Tom Lane <tgl@sss.pgh.pa.us>
2020-03-30 15:48 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Alvaro Herrera <alvherre@2ndquadrant.com>
2020-03-31 07:21 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Michael Paquier <michael@paquier.xyz>
@ 2020-03-31 13:52 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2020-04-01 06:07 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Michael Paquier <michael@paquier.xyz>
1 sibling, 1 reply; 8+ messages in thread
From: Alvaro Herrera @ 2020-03-31 13:52 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; hghwng@gmail.com; pgsql-bugs@lists.postgresql.org
On 2020-Mar-31, Michael Paquier wrote:
> On Mon, Mar 30, 2020 at 12:48:51PM -0300, Alvaro Herrera wrote:
> > On 2020-Mar-30, Tom Lane wrote:
> >> It looks to me like there's a similar hazard a bit further down
> >> (line 3029):
> >>
> >> appendConnStrVal(&connstr, PQdb(o_conn));
> >>
> >> I wonder if we should force reuse_previous to false if there's
> >> no o_conn, rather than fixing this stuff piecemeal.
> >
> > That was my impression too.
>
> Good point. What do you think about the attached then?
Looks better :-)
--
Álvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: BUG #16330: psql accesses null pointer in connect.c:do_connect
2020-03-30 02:20 BUG #16330: psql accesses null pointer in connect.c:do_connect PG Bug reporting form <noreply@postgresql.org>
2020-03-30 07:19 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Michael Paquier <michael@paquier.xyz>
2020-03-30 15:35 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Tom Lane <tgl@sss.pgh.pa.us>
2020-03-30 15:48 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Alvaro Herrera <alvherre@2ndquadrant.com>
2020-03-31 07:21 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Michael Paquier <michael@paquier.xyz>
2020-03-31 13:52 ` Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Alvaro Herrera <alvherre@2ndquadrant.com>
@ 2020-04-01 06:07 ` Michael Paquier <michael@paquier.xyz>
0 siblings, 0 replies; 8+ messages in thread
From: Michael Paquier @ 2020-04-01 06:07 UTC (permalink / raw)
To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; hghwng@gmail.com; pgsql-bugs@lists.postgresql.org
On Tue, Mar 31, 2020 at 10:52:59AM -0300, Alvaro Herrera wrote:
> Looks better :-)
Thanks to both of you for the suggestions and the reviews. Fix this
way then.
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 8+ messages in thread
end of thread, other threads:[~2020-04-01 06:07 UTC | newest]
Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-03-30 02:20 BUG #16330: psql accesses null pointer in connect.c:do_connect PG Bug reporting form <noreply@postgresql.org>
2020-03-30 07:19 ` Michael Paquier <michael@paquier.xyz>
2020-03-30 15:35 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-03-30 15:48 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2020-03-31 07:21 ` Michael Paquier <michael@paquier.xyz>
2020-03-31 13:43 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-03-31 13:52 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2020-04-01 06:07 ` Michael Paquier <michael@paquier.xyz>
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