Skip to content

Commit 9e0a97f

Browse files
committed
libpq: change PQconndefaults() to ignore invalid service files
Previously missing or invalid service files returned NULL. Also fix pg_upgrade to report "out of memory" for a null return from PQconndefaults(). Patch by Steve Singer, rewritten by me
1 parent 95e3d50 commit 9e0a97f

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

contrib/pg_upgrade/server.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ check_pghost_envvar(void)
325325

326326
start = PQconndefaults();
327327

328+
if (!start)
329+
pg_fatal("out of memory\n");
330+
328331
for (option = start; option->keyword != NULL; option++)
329332
{
330333
if (option->envvar && (strcmp(option->envvar, "PGHOST") == 0 ||

doc/src/sgml/libpq.sgml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,8 @@ typedef struct
483483
with an entry having a null <structfield>keyword</> pointer. The
484484
null pointer is returned if memory could not be allocated. Note that
485485
the current default values (<structfield>val</structfield> fields)
486-
will depend on environment variables and other context. Callers
486+
will depend on environment variables and other context. A
487+
missing or invalid service file will be silently ignored. Callers
487488
must treat the connection options data as read-only.
488489
</para>
489490

src/interfaces/libpq/fe-auth.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ pg_fe_sendauth(AuthRequest areq, PGconn *conn)
982982
* if there is an error, return NULL with an error message in errorMessage
983983
*/
984984
char *
985-
pg_fe_getauthname(PQExpBuffer errorMessage)
985+
pg_fe_getauthname(void)
986986
{
987987
const char *name = NULL;
988988
char *authn;

src/interfaces/libpq/fe-auth.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919

2020

2121
extern int pg_fe_sendauth(AuthRequest areq, PGconn *conn);
22-
extern char *pg_fe_getauthname(PQExpBuffer errorMessage);
22+
extern char *pg_fe_getauthname(void);
2323

2424
#endif /* FE_AUTH_H */

src/interfaces/libpq/fe-connect.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,8 @@ PQconndefaults(void)
875875
connOptions = conninfo_init(&errorBuf);
876876
if (connOptions != NULL)
877877
{
878-
if (!conninfo_add_defaults(connOptions, &errorBuf))
878+
/* pass NULL errorBuf to ignore errors */
879+
if (!conninfo_add_defaults(connOptions, NULL))
879880
{
880881
PQconninfoFree(connOptions);
881882
connOptions = NULL;
@@ -4412,9 +4413,10 @@ conninfo_array_parse(const char *const * keywords, const char *const * values,
44124413
*
44134414
* Defaults are obtained from a service file, environment variables, etc.
44144415
*
4415-
* Returns TRUE if successful, otherwise FALSE; errorMessage is filled in
4416-
* upon failure. Note that failure to locate a default value is not an
4417-
* error condition here --- we just leave the option's value as NULL.
4416+
* Returns TRUE if successful, otherwise FALSE; errorMessage, if supplied,
4417+
* is filled in upon failure. Note that failure to locate a default value
4418+
* is not an error condition here --- we just leave the option's value as
4419+
* NULL.
44184420
*/
44194421
static bool
44204422
conninfo_add_defaults(PQconninfoOption *options, PQExpBuffer errorMessage)
@@ -4424,9 +4426,10 @@ conninfo_add_defaults(PQconninfoOption *options, PQExpBuffer errorMessage)
44244426

44254427
/*
44264428
* If there's a service spec, use it to obtain any not-explicitly-given
4427-
* parameters.
4429+
* parameters. Ignore error if no error message buffer is passed
4430+
* because there is no way to pass back the failure message.
44284431
*/
4429-
if (parseServiceInfo(options, errorMessage) != 0)
4432+
if (parseServiceInfo(options, errorMessage) != 0 && errorMessage)
44304433
return false;
44314434

44324435
/*
@@ -4448,8 +4451,9 @@ conninfo_add_defaults(PQconninfoOption *options, PQExpBuffer errorMessage)
44484451
option->val = strdup(tmp);
44494452
if (!option->val)
44504453
{
4451-
printfPQExpBuffer(errorMessage,
4452-
libpq_gettext("out of memory\n"));
4454+
if (errorMessage)
4455+
printfPQExpBuffer(errorMessage,
4456+
libpq_gettext("out of memory\n"));
44534457
return false;
44544458
}
44554459
continue;
@@ -4465,8 +4469,9 @@ conninfo_add_defaults(PQconninfoOption *options, PQExpBuffer errorMessage)
44654469
option->val = strdup(option->compiled);
44664470
if (!option->val)
44674471
{
4468-
printfPQExpBuffer(errorMessage,
4469-
libpq_gettext("out of memory\n"));
4472+
if (errorMessage)
4473+
printfPQExpBuffer(errorMessage,
4474+
libpq_gettext("out of memory\n"));
44704475
return false;
44714476
}
44724477
continue;
@@ -4477,7 +4482,7 @@ conninfo_add_defaults(PQconninfoOption *options, PQExpBuffer errorMessage)
44774482
*/
44784483
if (strcmp(option->keyword, "user") == 0)
44794484
{
4480-
option->val = pg_fe_getauthname(errorMessage);
4485+
option->val = pg_fe_getauthname();
44814486
continue;
44824487
}
44834488
}

0 commit comments

Comments
 (0)