Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Fix crash in psql when attempting to reuse old connection
authorMichael Paquier <michael@paquier.xyz>
Wed, 1 Apr 2020 05:45:57 +0000 (14:45 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 1 Apr 2020 05:45:57 +0000 (14:45 +0900)
In a psql session, if the connection to the server is abruptly cut, the
referenced connection would become NULL as of CheckConnection().  This
could cause a hard crash with psql if attempting to connect by reusing
the past connection's data because of a null-pointer dereference with
either PQhost() or PQdb().  This issue is fixed by making sure that no
reuse of the past connection is done if it does not exist.

Issue has been introduced by 6e5f8d4, so backpatch down to 12.

Reported-by: Hugh Wang
Author: Michael Paquier
Reviewed-by: Álvaro Herrera, Tom Lane
Discussion: https://postgr.es/m/16330-b34835d83619e25d@postgresql.org
Backpatch-through: 12

src/bin/psql/command.c

index 389626285c8cb4314666c6a3e329805bb31b8e0b..3c34d9ca5db0bd0bd6be33e9ee83e246dfbde4af 100644 (file)
@@ -2941,6 +2941,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)
    {