ecpg: Fix NULL pointer dereference during connection lookup
authorMichael Paquier <michael@paquier.xyz>
Tue, 22 Jul 2025 05:00:12 +0000 (14:00 +0900)
committerMichael Paquier <michael@paquier.xyz>
Tue, 22 Jul 2025 05:00:12 +0000 (14:00 +0900)
ECPGconnect() caches established connections to the server, supporting
the case of a NULL connection name when a database name is not specified
by its caller.

A follow-up call to ECPGget_PGconn() to get an established connection
from the cached set with a non-NULL name could cause a NULL pointer
dereference if a NULL connection was listed in the cache and checked for
a match.  At least two connections are necessary to reproduce the issue:
one with a NULL name and one with a non-NULL name.

Author:  Aleksander Alekseev <aleksander@tigerdata.com>
Discussion: http://postgr.es/m/CAJ7c6TNvFTPUTZQuNAoqgzaSGz-iM4XR61D7vEj5PsQXwg2RyA@mail.gmail.com
Backpatch-through: 13

src/interfaces/ecpg/ecpglib/connect.c

index c673a9b976d04410fc13077c9024003ea565c170..8ab9201754f30043aacec5b4af40eb2bb2c8f947 100644 (file)
@@ -66,7 +66,12 @@ ecpg_get_connection_nr(const char *connection_name)
 
        for (con = all_connections; con != NULL; con = con->next)
        {
-           if (strcmp(connection_name, con->name) == 0)
+           /*
+            * Check for the case of a NULL connection name, stored as such in
+            * the connection information by ECPGconnect() when the database
+            * name is not specified by its caller.
+            */
+           if (con->name != NULL && strcmp(connection_name, con->name) == 0)
                break;
        }
        ret = con;