postgres.git / summary / log / commit / refs

commit    bfa9b25c94fe65411e29c2798b6d3da5fa6e9158
Author:   Tom Lane <tgl@sss.pgh.pa.us>
Date:     Thu Jul 17 16:46:38 2025 +0000
First released in: 19.0, 13.22, 14.19, 15.14, 16.10, 17.6, 18.0

    Fix PQport to never return NULL unless the connection is NULL.
    
    This is the documented behavior, and it worked that way before
    v10.  However, addition of the connhost[] array created cases
    where conn->connhost[conn->whichhost].port is NULL.  The rest
    of libpq is careful to substitute DEF_PGPORT[_STR] for a null
    or empty port string, but we failed to do so here, leading to
    possibly returning NULL.  As of v18 that causes psql's \conninfo
    command to segfault.  Older psql versions avoid that, but it's
    pretty likely that other clients have trouble with this,
    so we'd better back-patch the fix.
    
    In stable branches, just revert to our historical behavior of
    returning an empty string when there was no user-given port
    specification.  However, it seems substantially more useful and
    indeed more correct to hand back DEF_PGPORT_STR in such cases,
    so let's make v18 and master do that.
    
    Author: Daniele Varrazzo <daniele.varrazzo@gmail.com>
    Reviewed-by: Laurenz Albe <laurenz.albe@cybertec.at>
    Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
    Discussion: https://postgr.es/m/CA+mi_8YTS8WPZPO0PAb2aaGLwHuQ0DEQRF0ZMnvWss4y9FwDYQ@mail.gmail.com
    Backpatch-through: 13


src/interfaces/libpq/fe-connect.c | 6 ++++-- src/interfaces/libpq/libpq-int.h | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index 09eb79812ac..d9e3da04958 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -7528,10 +7528,12 @@ PQport(const PGconn *conn) if (!conn) return NULL; - if (conn->connhost != NULL) + if (conn->connhost != NULL && + conn->connhost[conn->whichhost].port != NULL && + conn->connhost[conn->whichhost].port[0] != '\0') return conn->connhost[conn->whichhost].port; - return ""; + return DEF_PGPORT_STR; } /* diff --git a/src/interfaces/libpq/libpq-int.h b/src/interfaces/libpq/libpq-int.h index a6cfd7f5c9d..f975f03ca1e 100644 --- a/src/interfaces/libpq/libpq-int.h +++ b/src/interfaces/libpq/libpq-int.h @@ -357,7 +357,8 @@ typedef struct pg_conn_host pg_conn_host_type type; /* type of host address */ char *host; /* host name or socket path */ char *hostaddr; /* host numeric IP address */ - char *port; /* port number (always provided) */ + char *port; /* port number (if NULL or empty, use + * DEF_PGPORT[_STR]) */ char *password; /* password for this host, read from the * password file; NULL if not sought or not * found in password file. */ [parent: e0d3f3cfb6f2]