Commit 9e0a97f1 authored by Bruce Momjian's avatar Bruce Momjian

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
parent 95e3d505
...@@ -325,6 +325,9 @@ check_pghost_envvar(void) ...@@ -325,6 +325,9 @@ check_pghost_envvar(void)
start = PQconndefaults(); start = PQconndefaults();
if (!start)
pg_fatal("out of memory\n");
for (option = start; option->keyword != NULL; option++) for (option = start; option->keyword != NULL; option++)
{ {
if (option->envvar && (strcmp(option->envvar, "PGHOST") == 0 || if (option->envvar && (strcmp(option->envvar, "PGHOST") == 0 ||
......
...@@ -483,7 +483,8 @@ typedef struct ...@@ -483,7 +483,8 @@ typedef struct
with an entry having a null <structfield>keyword</> pointer. The with an entry having a null <structfield>keyword</> pointer. The
null pointer is returned if memory could not be allocated. Note that null pointer is returned if memory could not be allocated. Note that
the current default values (<structfield>val</structfield> fields) the current default values (<structfield>val</structfield> fields)
will depend on environment variables and other context. Callers will depend on environment variables and other context. A
missing or invalid service file will be silently ignored. Callers
must treat the connection options data as read-only. must treat the connection options data as read-only.
</para> </para>
......
...@@ -982,7 +982,7 @@ pg_fe_sendauth(AuthRequest areq, PGconn *conn) ...@@ -982,7 +982,7 @@ pg_fe_sendauth(AuthRequest areq, PGconn *conn)
* if there is an error, return NULL with an error message in errorMessage * if there is an error, return NULL with an error message in errorMessage
*/ */
char * char *
pg_fe_getauthname(PQExpBuffer errorMessage) pg_fe_getauthname(void)
{ {
const char *name = NULL; const char *name = NULL;
char *authn; char *authn;
......
...@@ -19,6 +19,6 @@ ...@@ -19,6 +19,6 @@
extern int pg_fe_sendauth(AuthRequest areq, PGconn *conn); extern int pg_fe_sendauth(AuthRequest areq, PGconn *conn);
extern char *pg_fe_getauthname(PQExpBuffer errorMessage); extern char *pg_fe_getauthname(void);
#endif /* FE_AUTH_H */ #endif /* FE_AUTH_H */
...@@ -875,7 +875,8 @@ PQconndefaults(void) ...@@ -875,7 +875,8 @@ PQconndefaults(void)
connOptions = conninfo_init(&errorBuf); connOptions = conninfo_init(&errorBuf);
if (connOptions != NULL) if (connOptions != NULL)
{ {
if (!conninfo_add_defaults(connOptions, &errorBuf)) /* pass NULL errorBuf to ignore errors */
if (!conninfo_add_defaults(connOptions, NULL))
{ {
PQconninfoFree(connOptions); PQconninfoFree(connOptions);
connOptions = NULL; connOptions = NULL;
...@@ -4412,9 +4413,10 @@ conninfo_array_parse(const char *const * keywords, const char *const * values, ...@@ -4412,9 +4413,10 @@ conninfo_array_parse(const char *const * keywords, const char *const * values,
* *
* Defaults are obtained from a service file, environment variables, etc. * Defaults are obtained from a service file, environment variables, etc.
* *
* Returns TRUE if successful, otherwise FALSE; errorMessage is filled in * Returns TRUE if successful, otherwise FALSE; errorMessage, if supplied,
* upon failure. Note that failure to locate a default value is not an * is filled in upon failure. Note that failure to locate a default value
* error condition here --- we just leave the option's value as NULL. * is not an error condition here --- we just leave the option's value as
* NULL.
*/ */
static bool static bool
conninfo_add_defaults(PQconninfoOption *options, PQExpBuffer errorMessage) conninfo_add_defaults(PQconninfoOption *options, PQExpBuffer errorMessage)
...@@ -4424,9 +4426,10 @@ conninfo_add_defaults(PQconninfoOption *options, PQExpBuffer errorMessage) ...@@ -4424,9 +4426,10 @@ conninfo_add_defaults(PQconninfoOption *options, PQExpBuffer errorMessage)
/* /*
* If there's a service spec, use it to obtain any not-explicitly-given * If there's a service spec, use it to obtain any not-explicitly-given
* parameters. * parameters. Ignore error if no error message buffer is passed
* because there is no way to pass back the failure message.
*/ */
if (parseServiceInfo(options, errorMessage) != 0) if (parseServiceInfo(options, errorMessage) != 0 && errorMessage)
return false; return false;
/* /*
...@@ -4448,6 +4451,7 @@ conninfo_add_defaults(PQconninfoOption *options, PQExpBuffer errorMessage) ...@@ -4448,6 +4451,7 @@ conninfo_add_defaults(PQconninfoOption *options, PQExpBuffer errorMessage)
option->val = strdup(tmp); option->val = strdup(tmp);
if (!option->val) if (!option->val)
{ {
if (errorMessage)
printfPQExpBuffer(errorMessage, printfPQExpBuffer(errorMessage,
libpq_gettext("out of memory\n")); libpq_gettext("out of memory\n"));
return false; return false;
...@@ -4465,6 +4469,7 @@ conninfo_add_defaults(PQconninfoOption *options, PQExpBuffer errorMessage) ...@@ -4465,6 +4469,7 @@ conninfo_add_defaults(PQconninfoOption *options, PQExpBuffer errorMessage)
option->val = strdup(option->compiled); option->val = strdup(option->compiled);
if (!option->val) if (!option->val)
{ {
if (errorMessage)
printfPQExpBuffer(errorMessage, printfPQExpBuffer(errorMessage,
libpq_gettext("out of memory\n")); libpq_gettext("out of memory\n"));
return false; return false;
...@@ -4477,7 +4482,7 @@ conninfo_add_defaults(PQconninfoOption *options, PQExpBuffer errorMessage) ...@@ -4477,7 +4482,7 @@ conninfo_add_defaults(PQconninfoOption *options, PQExpBuffer errorMessage)
*/ */
if (strcmp(option->keyword, "user") == 0) if (strcmp(option->keyword, "user") == 0)
{ {
option->val = pg_fe_getauthname(errorMessage); option->val = pg_fe_getauthname();
continue; continue;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment