Re: Fix PQport to never return NULL if the connection is valid
Re: Fix PQport to never return NULL if the connection is valid
От:
Laurenz Albe <laurenz.albe@cybertec.at>
Дата:
On Thu, 2025-05-08 at 22:01 +0200, Daniele Varrazzo wrote: > I looked a bit more into the meaning of the port="" setting. The docs > for the port parameter / PGPORT env var > > say: > > An empty string, or an empty item in a comma-separated list, > specifies the default port number established when > PostgreSQL was built > > Thus I understand that the return value of PQport wants to reflect > this behaviour, therefore the value "" is legitimate and it's up to > the client to figure out how the libpq was built (PQconndefaults may > tell that). > > Please find attached a new patch that doesn't change the behaviour and > just makes sure to not return NULL in case no info is available in > 'conn->connhost'. I think that it is important to fix that bug and to backpatch it. Without the patch, I can produce the following crash: $ psql port= psql (18beta1) Type "help" for help. test=> \conninfo Segmentation fault (core dumped) > diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c > --- a/src/interfaces/libpq/fe-connect.c > +++ b/src/interfaces/libpq/fe-connect.c > @@ -7574,7 +7574,9 @@ 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 ""; You should omit the third test. If the first character of the port string is 0, the string is empty, and you might as well return it instead of a static empty string. The patch applies (with considerable offset), builds well and passes the regression tests. The latter is unsurprising, since nothing tests for that. I wondered if it was worth adding a test, but could not find a convenient place. Adding a TAP test seems a bit out of proportion for a small fix like that. Status set to "waiting on author". Yours, Laurenz Albe
Re: Fix PQport to never return NULL if the connection is valid
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Laurenz Albe writes:
> On Thu, 2025-05-08 at 22:01 +0200, Daniele Varrazzo wrote:
>> Please find attached a new patch that doesn't change the behaviour and
>> just makes sure to not return NULL in case no info is available in
>> 'conn->connhost'.
> I think that it is important to fix that bug and to backpatch it.
> Without the patch, I can produce the following crash:
> $ psql port=
> psql (18beta1)
> Type "help" for help.
> test=> \conninfo
> Segmentation fault (core dumped)
This crash is actually new in v18; earlier versions produce something
like
psql (17.5)
Type "help" for help.
postgres=# \conninfo
You are connected to database "postgres" as user "postgres" via socket in "/tmp" at port "(null)".
postgres=#
because the code was basically
printf(_("You are connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n"),
db, PQuser(pset.db), host, PQport(pset.db));
and fortunately our implementation of printf is guaranteed not to
crash on a null string pointer.
Having said that, I agree with back-patching Daniele's fix, because it
is making the code agree with what happened before we introduced the
connhost[] data structure in v10, and also with the documentation:
returns NULL if the
conn argument is NULL.
Otherwise, if there is an error producing the port information (perhaps
if the connection has not been fully established or there was an
error), it returns an empty string.
However ... I wonder if we ought to take the opportunity to fix
\conninfo to ensure that it prints something useful rather than
just an empty string? The upgrade to make it print in tabular
format is what broke this, but it also seems like an excuse to
improve the functionality as well as the cosmetics.
However, fixing this in \conninfo seems a bit tedious: we'd have
to grovel through the output of PQconndefaults. (I don't think
it's okay for psql to assume that it can rely on having been
built with the same value of DEF_PGPORT_STR that the libpq it's
linked to is using.) 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.
regards, tom lane
Re: Fix PQport to never return NULL if the connection is valid
От:
Laurenz Albe <laurenz.albe@cybertec.at>
Дата:
On Wed, 2025-07-16 at 15:36 -0400, Tom Lane wrote: > Laurenz Albe writes: > > On Thu, 2025-05-08 at 22:01 +0200, Daniele Varrazzo wrote: > > > Please find attached a new patch that doesn't change the behaviour and > > > just makes sure to not return NULL in case no info is available in > > > 'conn->connhost'. > > > I think that it is important to fix that bug and to backpatch it. > > Without the patch, I can produce the following crash: > > > $ psql port= > > psql (18beta1) > > Type "help" for help. > > > test=> \conninfo > > Segmentation fault (core dumped) > > This crash is actually new in v18 That is a relief. > Having said that, I agree with back-patching Daniele's fix > However ... I wonder if we ought to take the opportunity to fix > \conninfo to ensure that it prints something useful rather than > just an empty string? The upgrade to make it print in tabular > format is what broke this, but it also seems like an excuse to > improve the functionality as well as the cosmetics. > > However, fixing this in \conninfo seems a bit tedious: we'd have > to grovel through the output of PQconndefaults. (I don't think > it's okay for psql to assume that it can rely on having been > built with the same value of DEF_PGPORT_STR that the libpq it's > linked to is using.) 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. As long as the information is factually correct, it is a good idea to have \conninfo return more complete data. Yours, Laurenz Albe
Re: Fix PQport to never return NULL if the connection is valid
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Laurenz Albe 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