Fix read-after-free bug in error handler.
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Wed, 21 Jan 2015 16:44:52 +0000 (18:44 +0200)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Wed, 21 Jan 2015 16:47:30 +0000 (18:47 +0200)
If a connection dies, CC_on_abort() calls PQfinish() which frees the error
message. handle_pgres_error better deal with the error message before
calling CC_on_abort().

Spotted by valgrind, on the "diagnostic" regression test.

connection.c

index e88a30a5ed406eebea693f2af056275d976cec3d..1ef123604ab4da2638e18fd20d2c088d49069b46 100644 (file)
@@ -867,13 +867,6 @@ handle_pgres_error(ConnectionClass *self, const PGresult *pgres,
 
    mylog("notice/error message len=%d\n", strlen(errmsg));
 
-   if (0 != abort_opt
-#ifdef _LEGACY_MODE_
-       || TRUE
-#endif /* _LEGACY_NODE_ */
-       )
-       CC_on_abort(self, abort_opt);
-
    if (fatal)
    {
        if (res)
@@ -896,6 +889,13 @@ handle_pgres_error(ConnectionClass *self, const PGresult *pgres,
    if (errmsg != errprimary)
        free(errmsg);
 
+   if (0 != abort_opt
+#ifdef _LEGACY_MODE_
+       || TRUE
+#endif /* _LEGACY_NODE_ */
+       )
+       CC_on_abort(self, abort_opt);
+
    LIBPQ_update_transaction_status(self);
 }