From be3a051e8f39ad504cd22e22e80178d57949b7d9 Mon Sep 17 00:00:00 2001
From: Daniele Varrazzo <daniele.varrazzo@gmail.com>
Date: Thu, 8 May 2025 18:34:56 +0200
Subject: [PATCH] Fix PQport to never return NULL unless the connection is NULL

This is the documented behaviour, but, at the moment, if the port is an
empty string, the port is returned null.

Return the default port instead of an empty string if the port is not
set.
---
 src/interfaces/libpq/fe-connect.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 2f87961a71e..7f746aef94e 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -7074,10 +7074,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;
 }
 
 /*
-- 
2.34.1

