diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2022-06-06 11:20:21 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2022-06-06 11:20:36 -0400 |
commit | 16d68007cd7400061bd499b92eb80fbb9798362c (patch) | |
tree | 2433e32517f7af5082b1a0ad95e73430e1a0681c /src | |
parent | b364cfdfaf6b24e18c8f6017111306259f87c0f4 (diff) | |
download | postgresql-16d68007cd7400061bd499b92eb80fbb9798362c.tar.gz postgresql-16d68007cd7400061bd499b92eb80fbb9798362c.zip |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/interfaces/ecpg/ecpglib/error.c | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/src/interfaces/ecpg/ecpglib/error.c b/src/interfaces/ecpg/ecpglib/error.c index a4e3c0d01f8..6e04c95fca1 100644 --- a/src/interfaces/ecpg/ecpglib/error.c +++ b/src/interfaces/ecpg/ecpglib/error.c @@ -229,18 +229,17 @@ ecpg_raise_backend(int line, PGresult *result, PGconn *conn, int compat) return; } - if (result) - { - sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE); - if (sqlstate == NULL) - sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR; - message = PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY); - } - else - { + /* + * PQresultErrorField will return NULL if "result" is NULL, or if there is + * no such field, which will happen for libpq-generated errors. Fall back + * to PQerrorMessage in such cases. + */ + sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE); + if (sqlstate == NULL) sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR; + message = PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY); + if (message == NULL) message = PQerrorMessage(conn); - } if (strcmp(sqlstate, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR) == 0) { |