Re: Fix PQport to never return NULL if the connection is valid
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Laurenz Albe <laurenz.albe@cybertec.at>
Cc: Daniele Varrazzo <daniele.varrazzo@gmail.com>,
PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Date: 2025-07-17T16:58:36Z
Lists: pgsql-hackers
Laurenz Albe <laurenz.albe@cybertec.at> writes:
> On Wed, 2025-07-16 at 15:36 -0400, Tom Lane wrote:
>> ... So it seems attractive to stick with the
>> original thought of making PQport substitute the correct value
>> for an empty string. PQport *does* know the correct value of
>> DEF_PGPORT_STR, so it'd be pretty nearly a one-liner to make
>> this happen there.
>>
>> I'd only propose doing this in v18/HEAD, and sticking to the
>> v2 patch in the stable branches.
> That makes a lot of sense.
Cool, done that way.
I ended up using Daniele's patches verbatim -- the first one in
v18/HEAD and the second in older branches. There was some discussion
of whether v18/HEAD should return an empty string if the connection is
bad, but I don't think that'd be helpful; the other inquiry functions
don't act that way. (But I left in the documentation text claiming
that we might do so, since perhaps such behavior would become
necessary in future.)
Also I didn't bother to remove the test for whether the port string
is empty. As you noted, it's not really necessary in the older
branches, but it is needed in v18/HEAD and it seemed better to
keep the code looking as similar as possible.
I dug around and could not find any indication of similar bugs
elsewhere in libpq. I did find that the documentation of the
port field was a lie:
char *port; /* port number (always provided) */
so I fixed that. This might be archaeological evidence of how
the bug came to be: apparently the original intent was for this
field to always be accurate, and when that changed, PQport
did not get the memo.
regards, tom lane
Commits
-
Fix PQport to never return NULL unless the connection is NULL.
- d5cba774626f 14.19 landed
- 9dcd1aa815f5 13.22 landed
- a372a64db794 15.14 landed
- 3f10d2b66544 17.6 landed
- 009c20a3da5a 16.10 landed
- daf9bdc47d11 19 (unreleased) landed
- bfa9b25c94fe 18.0 landed