postgres.git / summary / log / commit / refs

commit    0ded7615d8cafbb1a5ae3d0ba0a931eb146e63a5
Author:   Michael Paquier <michael@paquier.xyz>
Date:     Tue Jul 22 05:00:04 2025 +0000
First released in: 13.22, 14.19, 15.14, 16.10, 17.6, 18.0

    ecpg: Fix NULL pointer dereference during connection lookup
    
    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: https://postgr.es/m/CAJ7c6TNvFTPUTZQuNAoqgzaSGz-iM4XR61D7vEj5PsQXwg2RyA@mail.gmail.com
    Backpatch-through: 13


src/interfaces/ecpg/ecpglib/connect.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/interfaces/ecpg/ecpglib/connect.c b/src/interfaces/ecpg/ecpglib/connect.c index 2bbb70333dc..713cbbf6360 100644 --- a/src/interfaces/ecpg/ecpglib/connect.c +++ b/src/interfaces/ecpg/ecpglib/connect.c @@ -58,7 +58,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; [parent: f9545e95c5e7]