1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-15 19:21:59 +03:00

Avoid improbable null pointer dereference in pgpassfileWarning().

Coverity complained that we might pass a null pointer to strcmp()
if PQresultErrorField were to return NULL.  That shouldn't be possible,
since the server is supposed to always provide some SQLSTATE or other
in an error message.  But we usually defend against such hazards, and
it only takes a little more code to do so here.

There's no good reason to think this is a live bug, so no back-patch.
This commit is contained in:
Tom Lane
2017-02-02 19:49:15 -05:00
parent 555494d1bc
commit 8ac0365c22

View File

@ -6312,22 +6312,23 @@ passwordFromFile(char *hostname, char *port, char *dbname,
/* /*
* If the connection failed, we should mention if * If the connection failed due to bad password, we should mention
* we got the password from the pgpassfile in case that * if we got the password from the pgpassfile.
* password is wrong.
*/ */
static void static void
pgpassfileWarning(PGconn *conn) pgpassfileWarning(PGconn *conn)
{ {
/* If it was 'invalid authorization', add pgpassfile mention */ /* If it was 'invalid authorization', add pgpassfile mention */
/* only works with >= 9.0 servers */ /* only works with >= 9.0 servers */
if (conn->pgpassfile_used && conn->password_needed && conn->result && if (conn->pgpassfile_used && conn->password_needed && conn->result)
strcmp(PQresultErrorField(conn->result, PG_DIAG_SQLSTATE),
ERRCODE_INVALID_PASSWORD) == 0)
{ {
appendPQExpBuffer(&conn->errorMessage, const char *sqlstate = PQresultErrorField(conn->result,
PG_DIAG_SQLSTATE);
if (sqlstate && strcmp(sqlstate, ERRCODE_INVALID_PASSWORD) == 0)
appendPQExpBuffer(&conn->errorMessage,
libpq_gettext("password retrieved from file \"%s\"\n"), libpq_gettext("password retrieved from file \"%s\"\n"),
conn->pgpassfile); conn->pgpassfile);
} }
} }