Skip to content

Commit 3f10d2b

Browse files
committed
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
1 parent 0c466f5 commit 3f10d2b

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7074,7 +7074,9 @@ PQport(const PGconn *conn)
70747074
if (!conn)
70757075
return NULL;
70767076

7077-
if (conn->connhost != NULL)
7077+
if (conn->connhost != NULL &&
7078+
conn->connhost[conn->whichhost].port != NULL &&
7079+
conn->connhost[conn->whichhost].port[0] != '\0')
70787080
return conn->connhost[conn->whichhost].port;
70797081

70807082
return "";

src/interfaces/libpq/libpq-int.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@ typedef struct pg_conn_host
351351
pg_conn_host_type type; /* type of host address */
352352
char *host; /* host name or socket path */
353353
char *hostaddr; /* host numeric IP address */
354-
char *port; /* port number (always provided) */
354+
char *port; /* port number (if NULL or empty, use
355+
* DEF_PGPORT[_STR]) */
355356
char *password; /* password for this host, read from the
356357
* password file; NULL if not sought or not
357358
* found in password file. */

0 commit comments

Comments
 (0)