]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-sql: pgsql - Return only last error
authorAki Tuomi <aki.tuomi@open-xchange.com>
Thu, 29 Jun 2023 10:36:36 +0000 (13:36 +0300)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Wed, 5 Jul 2023 07:21:08 +0000 (10:21 +0300)
PQerrorMessage() can return multiple lines, make sure we only
return the very last one.

src/lib-sql/driver-pgsql.c

index 2775b7eca131873d2048da6a3a3898229c3129ec..02ebf715725f6f0cf54eaef7e259c0069ff25fde 100644 (file)
@@ -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);
 }