From: Aki Tuomi Date: Thu, 29 Jun 2023 10:36:36 +0000 (+0300) Subject: lib-sql: pgsql - Return only last error X-Git-Tag: 2.4.0~2659 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f2d9603ba89247a076b2d05f055a9dc23dc7a5c0;p=thirdparty%2Fdovecot%2Fcore.git lib-sql: pgsql - Return only last error PQerrorMessage() can return multiple lines, make sure we only return the very last one. --- diff --git a/src/lib-sql/driver-pgsql.c b/src/lib-sql/driver-pgsql.c index 2775b7eca1..02ebf71572 100644 --- a/src/lib-sql/driver-pgsql.c +++ b/src/lib-sql/driver-pgsql.c @@ -156,15 +156,20 @@ static void driver_pgsql_close(struct pgsql_db *db) static const char *last_error(struct pgsql_db *db) { - const char *msg; + const char *msg, *pos, *orig; size_t len; - msg = PQerrorMessage(db->pg); + orig = msg = PQerrorMessage(db->pg); if (msg == NULL) return "(no error set)"; - /* Error message should contain trailing \n, we don't want it */ len = strlen(msg); + /* The error can contain multiple lines, but we only want the last */ + while ((pos = strchr(msg, '\n')) != NULL && (pos - orig) < (ptrdiff_t)len - 1) + msg = pos + 1; + + len = strlen(msg); + /* Error message should contain trailing \n, we don't want it */ return len == 0 || msg[len-1] != '\n' ? msg : t_strndup(msg, len-1); }