static int connectDBComplete(PGconn *conn);
static PGPing internal_ping(PGconn *conn);
static PGconn *makeEmptyPGconn(void);
-static void fillPGconn(PGconn *conn, PQconninfoOption *connOptions);
+static bool fillPGconn(PGconn *conn, PQconninfoOption *connOptions);
static void freePGconn(PGconn *conn);
static void closePGconn(PGconn *conn);
static PQconninfoOption *conninfo_init(PQExpBuffer errorMessage);
/*
* Move option values into conn structure
*/
- fillPGconn(conn, connOptions);
+ if (!fillPGconn(conn, connOptions))
+ {
+ PQconninfoFree(connOptions);
+ return conn;
+ }
/*
* Free the option info - all is in conn now
return conn;
}
-static void
+/*
+ * Move option values into conn structure
+ *
+ * Don't put anything cute here --- intelligence should be in
+ * connectOptions2 ...
+ *
+ * Returns true on success. On failure, returns false and sets error message.
+ */
+static bool
fillPGconn(PGconn *conn, PQconninfoOption *connOptions)
{
const internalPQconninfoOption *option;
- /*
- * Move option values into conn structure
- *
- * Don't put anything cute here --- intelligence should be in
- * connectOptions2 ...
- *
- * XXX: probably worth checking strdup() return value here...
- */
for (option = PQconninfoOptions; option->keyword; option++)
{
const char *tmp = conninfo_getval(connOptions, option->keyword);
if (*connmember)
free(*connmember);
- *connmember = tmp ? strdup(tmp) : NULL;
+ if (tmp)
+ {
+ *connmember = strdup(tmp);
+ if (*connmember == NULL)
+ {
+ printfPQExpBuffer(&conn->errorMessage,
+ libpq_gettext("out of memory\n"));
+ return false;
+ }
+ }
+ else
+ *connmember = NULL;
}
}
+
+ return true;
}
/*
/*
* Move option values into conn structure
*/
- fillPGconn(conn, connOptions);
+ if (!fillPGconn(conn, connOptions))
+ {
+ conn->status = CONNECTION_BAD;
+ PQconninfoFree(connOptions);
+ return false;
+ }
/*
* Free the option info - all is in conn now
if (conn->dbName)
free(conn->dbName);
conn->dbName = strdup(conn->pguser);
+ if (!conn->dbName)
+ goto oom_error;
}
/*
conn->pgpass = PasswordFromFile(conn->pghost, conn->pgport,
conn->dbName, conn->pguser);
if (conn->pgpass == NULL)
+ {
conn->pgpass = strdup(DefaultPassword);
+ if (!conn->pgpass)
+ goto oom_error;
+
+ }
else
conn->dot_pgpass_used = true;
}
#endif
}
else
+ {
conn->sslmode = strdup(DefaultSSLMode);
+ if (!conn->sslmode)
+ goto oom_error;
+ }
/*
* Resolve special "auto" client_encoding from the locale
{
free(conn->client_encoding_initial);
conn->client_encoding_initial = strdup(pg_encoding_to_char(pg_get_encoding_from_locale(NULL, true)));
+ if (!conn->client_encoding_initial)
+ goto oom_error;
}
/*
conn->options_valid = true;
return true;
+
+oom_error:
+ conn->status = CONNECTION_BAD;
+ printfPQExpBuffer(&conn->errorMessage,
+ libpq_gettext("out of memory\n"));
+ return false;
}
/*
if (conn->dbName)
free(conn->dbName);
conn->dbName = strdup(dbName);
+ if (!conn->dbName)
+ goto oom_error;
}
}
if (conn->pghost)
free(conn->pghost);
conn->pghost = strdup(pghost);
+ if (!conn->pghost)
+ goto oom_error;
}
if (pgport && pgport[0] != '\0')
if (conn->pgport)
free(conn->pgport);
conn->pgport = strdup(pgport);
+ if (!conn->pgport)
+ goto oom_error;
}
if (pgoptions && pgoptions[0] != '\0')
if (conn->pgoptions)
free(conn->pgoptions);
conn->pgoptions = strdup(pgoptions);
+ if (!conn->pgoptions)
+ goto oom_error;
}
if (pgtty && pgtty[0] != '\0')
if (conn->pgtty)
free(conn->pgtty);
conn->pgtty = strdup(pgtty);
+ if (!conn->pgtty)
+ goto oom_error;
}
if (login && login[0] != '\0')
if (conn->pguser)
free(conn->pguser);
conn->pguser = strdup(login);
+ if (!conn->pguser)
+ goto oom_error;
}
if (pwd && pwd[0] != '\0')
if (conn->pgpass)
free(conn->pgpass);
conn->pgpass = strdup(pwd);
+ if (!conn->pgpass)
+ goto oom_error;
}
/*
(void) connectDBComplete(conn);
return conn;
+
+oom_error:
+ conn->status = CONNECTION_BAD;
+ printfPQExpBuffer(&conn->errorMessage,
+ libpq_gettext("out of memory\n"));
+ return conn;
}
if (strcmp(options[i].keyword, optname) == 0)
{
if (options[i].val == NULL)
+ {
options[i].val = strdup(optval);
+ if (!options[i].val)
+ {
+ printfPQExpBuffer(errorMessage,
+ libpq_gettext("out of memory\n"));
+ free(result);
+ return 3;
+ }
+ }
found_keyword = true;
break;
}
{
if (options[i].val == NULL)
options[i].val = strdup(val);
+ if (!options[i].val)
+ {
+ printfPQExpBuffer(errorMessage,
+ libpq_gettext("out of memory\n"));
+ fclose(f);
+ return 3;
+ }
found_keyword = true;
break;
}
if (options[k].val)
free(options[k].val);
options[k].val = strdup(str_option->val);
+ if (!options[k].val)
+ {
+ printfPQExpBuffer(errorMessage,
+ libpq_gettext("out of memory\n"));
+ PQconninfoFree(options);
+ PQconninfoFree(dbname_options);
+ return NULL;
+ }
break;
}
}
{
int prefix_len;
char *p;
- char *buf = strdup(uri); /* need a modifiable copy of the input
- * URI */
- char *start = buf;
+ char *buf;
+ char *start;
char prevchar = '\0';
char *user = NULL;
char *host = NULL;
bool retval = false;
+ /* need a modifiable copy of the input URI */
+ buf = strdup(uri);
if (buf == NULL)
{
printfPQExpBuffer(errorMessage,
libpq_gettext("out of memory\n"));
return false;
}
+ start = buf;
/* Skip the URI prefix */
prefix_len = uri_prefix_length(uri);
static char *
conninfo_uri_decode(const char *str, PQExpBuffer errorMessage)
{
- char *buf = malloc(strlen(str) + 1);
- char *p = buf;
+ char *buf;
+ char *p;
const char *q = str;
+ buf = malloc(strlen(str) + 1);
if (buf == NULL)
{
printfPQExpBuffer(errorMessage, libpq_gettext("out of memory\n"));
return NULL;
}
+ p = buf;
for (;;)
{
else
{
value_copy = strdup(value);
-
if (value_copy == NULL)
{
printfPQExpBuffer(errorMessage, libpq_gettext("out of memory\n"));
ret = strdup(t);
fclose(fp);
+ if (!ret)
+ {
+ /* Out of memory. XXX: an error message would be nice. */
+ return NULL;
+ }
+
/* De-escape password. */
for (p1 = p2 = ret; *p1 != ':' && *p1 != '\0'; ++p1, ++p2)
{