fix use-after-free in postgres error message
authorFlorian Dold <florian.dold@gmail.com>
Sat, 11 Feb 2017 15:41:58 +0000 (16:41 +0100)
committerFlorian Dold <florian.dold@gmail.com>
Sat, 11 Feb 2017 15:41:58 +0000 (16:41 +0100)
src/postgres/postgres.c

index 4798c129d362393f0089d164a44146c35df6a27d..14095c5a4429dc3ba4bd2ad4162b7696d7627dab 100644 (file)
@@ -182,22 +182,22 @@ GNUNET_POSTGRES_connect (const struct GNUNET_CONFIGURATION_Handle * cfg,
                                             &conninfo))
     conninfo = NULL;
   dbh = PQconnectdb (conninfo == NULL ? "" : conninfo);
-  GNUNET_free_non_null (conninfo);
-  if (NULL == dbh)
-  {
-    /* FIXME: warn about out-of-memory? */
-    return NULL;
-  }
-  if (PQstatus (dbh) != CONNECTION_OK)
+
+  if (NULL != dbh)
   {
-    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
-                    "postgres",
-                     _("Unable to connect to Postgres database '%s': %s\n"),
-                     conninfo,
-                     PQerrorMessage (dbh));
-    PQfinish (dbh);
-    return NULL;
+    if (PQstatus (dbh) != CONNECTION_OK)
+    {
+      GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
+                       "postgres",
+                       _("Unable to connect to Postgres database '%s': %s\n"),
+                       conninfo,
+                       PQerrorMessage (dbh));
+      PQfinish (dbh);
+      dbh = NULL;
+    }
   }
+  // FIXME: warn about out-of-memory when dbh is NULL?
+  GNUNET_free_non_null (conninfo);
   return dbh;
 }