Don't leak malloc'd error string in libpqrcv_check_conninfo().
authorTom Lane <[email protected]>
Fri, 19 Mar 2021 02:21:58 +0000 (22:21 -0400)
committerTom Lane <[email protected]>
Fri, 19 Mar 2021 02:22:47 +0000 (22:22 -0400)
We leaked the error report from PQconninfoParse, when there was
one.  It seems unlikely that real usage patterns would repeat
the failure often enough to create serious bloat, but let's
back-patch anyway to keep the code similar in all branches.

Found via valgrind testing.
Back-patch to v10 where this code was added.

Discussion: https://postgr.es/m/3816764.1616104288@sss.pgh.pa.us

src/backend/replication/libpqwalreceiver/libpqwalreceiver.c

index f74378110a0082eb95ada92031c7f6ba1bd112b6..021c1b36f3ecbd3efa70b3be36e0a08d6081dc86 100644 (file)
@@ -246,9 +246,15 @@ libpqrcv_check_conninfo(const char *conninfo)
 
    opts = PQconninfoParse(conninfo, &err);
    if (opts == NULL)
+   {
+       /* The error string is malloc'd, so we must free it explicitly */
+       char       *errcopy = err ? pstrdup(err) : "out of memory";
+
+       PQfreemem(err);
        ereport(ERROR,
                (errcode(ERRCODE_SYNTAX_ERROR),
-                errmsg("invalid connection string syntax: %s", err)));
+                errmsg("invalid connection string syntax: %s", errcopy)));
+   }
 
    PQconninfoFree(opts);
 }