pgsql: Fix PQport to never return NULL unless the connection is NULL.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix PQport to never return NULL unless the connection is NULL.
Date: 2025-07-17 16:47:11
Message-ID: E1ucRlH-006mso-0D@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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(dot)varrazzo(at)gmail(dot)com>
Reviewed-by: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
Reviewed-by: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Discussion: https://postgr.es/m/CA+mi_8YTS8WPZPO0PAb2aaGLwHuQ0DEQRF0ZMnvWss4y9FwDYQ@mail.gmail.com
Backpatch-through: 13

Branch
------
REL_13_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/9dcd1aa815f5911bc7d526cdfee85ae493b5e12c

Modified Files
--------------
src/interfaces/libpq/fe-connect.c | 4 +++-
src/interfaces/libpq/libpq-int.h | 3 ++-
2 files changed, 5 insertions(+), 2 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Álvaro Herrera 2025-07-17 16:57:21 Re: pgsql: Remove assertion from PortalRunMulti
Previous Message Aleksander Alekseev 2025-07-17 16:11:36 Re: pgsql: Remove assertion from PortalRunMulti