Commit a5dbca46 authored by Tom Lane's avatar Tom Lane

Don't fail on libpq-generated error reports in ecpg_raise_backend().

An error PGresult generated by libpq itself, such as a report of
connection loss, won't have broken-down error fields.
ecpg_raise_backend() blithely assumed that PG_DIAG_MESSAGE_PRIMARY
would always be present, and would end up passing a NULL string
pointer to snprintf when it isn't.  That would typically crash
before 3779ac62d, and it would fail to provide a useful error report
in any case.  Best practice is to substitute PQerrorMessage(conn)
in such cases, so do that.

Per bug #17421 from Masayuki Hirose.  Back-patch to all supported
branches.

Discussion: https://postgr.es/m/17421-790ff887e3188874@postgresql.org
parent a04ccf6d
...@@ -229,18 +229,17 @@ ecpg_raise_backend(int line, PGresult *result, PGconn *conn, int compat) ...@@ -229,18 +229,17 @@ ecpg_raise_backend(int line, PGresult *result, PGconn *conn, int compat)
return; return;
} }
if (result) /*
{ * PQresultErrorField will return NULL if "result" is NULL, or if there is
sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE); * no such field, which will happen for libpq-generated errors. Fall back
if (sqlstate == NULL) * to PQerrorMessage in such cases.
sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR; */
message = PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY); sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE);
} if (sqlstate == NULL)
else
{
sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR; sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR;
message = PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY);
if (message == NULL)
message = PQerrorMessage(conn); message = PQerrorMessage(conn);
}
if (strcmp(sqlstate, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR) == 0) if (strcmp(sqlstate, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR) == 0)
{ {
......
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