Skip to content

Commit 6d157e7

Browse files
committed
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 3779ac6, 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
1 parent 157f873 commit 6d157e7

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/interfaces/ecpg/ecpglib/error.c

+9-10
Original file line numberDiff line numberDiff line change
@@ -229,18 +229,17 @@ ecpg_raise_backend(int line, PGresult *result, PGconn *conn, int compat)
229229
return;
230230
}
231231

232-
if (result)
233-
{
234-
sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE);
235-
if (sqlstate == NULL)
236-
sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR;
237-
message = PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY);
238-
}
239-
else
240-
{
232+
/*
233+
* PQresultErrorField will return NULL if "result" is NULL, or if there is
234+
* no such field, which will happen for libpq-generated errors. Fall back
235+
* to PQerrorMessage in such cases.
236+
*/
237+
sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE);
238+
if (sqlstate == NULL)
241239
sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR;
240+
message = PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY);
241+
if (message == NULL)
242242
message = PQerrorMessage(conn);
243-
}
244243

245244
if (strcmp(sqlstate, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR) == 0)
246245
{

0 commit comments

Comments
 (0)