Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WEWRK-00064G-51 for pgsql-bugs@arkaria.postgresql.org; Sat, 15 Feb 2014 04:01:19 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WEWRJ-0008CO-Ci for pgsql-bugs@arkaria.postgresql.org; Sat, 15 Feb 2014 04:01:17 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WEWRI-0008C8-CL for pgsql-bugs@postgresql.org; Sat, 15 Feb 2014 04:01:16 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WEWRE-00017q-Nh for pgsql-bugs@postgresql.org; Sat, 15 Feb 2014 04:01:15 +0000 Received: from bruce by momjian.us with local (Exim 4.72) (envelope-from ) id 1WEWRD-00054f-Es; Fri, 14 Feb 2014 23:01:11 -0500 Date: Fri, 14 Feb 2014 23:01:11 -0500 From: Bruce Momjian To: Benjamin Wassermann Cc: pgsql-bugs@postgresql.org Subject: Re: BUG #8461: PostgreSQL 9.3 pg_dump heap corruptions Message-ID: <20140215040111.GA15047@momjian.us> References: <1381753906060-5774487.post@n5.nabble.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="5vNYLRcllDrimb99" Content-Disposition: inline In-Reply-To: <1381753906060-5774487.post@n5.nabble.com> User-Agent: Mutt/1.5.20 (2009-06-14) X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --5vNYLRcllDrimb99 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Mon, Oct 14, 2013 at 05:31:46AM -0700, Benjamin Wassermann wrote: > We finally find out why this problem occurs. > > PG_dump use some Functions like > initPQExpBuffer(..) > from the libpq.dll. > > In this function "initPQExpBuffer(...)" are some memory allocated with > malloc(...). > (File: "pg_dump.c", line 9366) > > After the function is successfully dumped to backup file, there is a > "free(...)" in Function "dumpFunc" at line 9942 ff. > ... > free(funcsig); > ... > > but the PG_dump.exe cant free memory which is allocated by libpq.dll. > > To fix this problem the "libpq.dll" need a new function named > "deletePQCharPointer()" Actually, there are many places where Windows binaries free memory allocated by libpq. This is possible because of the way we compile libpq and the binary. Our PQfreemem() manual page has the details It is particularly important that this function, rather than free(), be used on Microsoft Windows. This is because allocating memory in a DLL and releasing it in the application works only if multithreaded/single-threaded, release/debug, and static/dynamic flags are the same for the DLL and the application. On non-Microsoft Windows platforms, this function is the same as the standard library function free(). If the free() was not working for you, my guess is that you had mismatched flags for libpq and pg_dump compiles. However, you were right that there was a memory leak in that area of the code related to how funcfullsig and aggfullsig were conditionally assigned values, but not freed. The attached patch fixes this memory leak; not sure if this should be back-patched. -- Bruce Momjian http://momjian.us EnterpriseDB http://enterprisedb.com + Everyone has their own god. + --5vNYLRcllDrimb99 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="pg_dump.diff" diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c new file mode 100644 index 458a118..fd0bf4d *** a/src/bin/pg_dump/pg_dump.c --- b/src/bin/pg_dump/pg_dump.c *************** dumpFunc(Archive *fout, FuncInfo *finfo) *** 9608,9614 **** PQExpBuffer asPart; PGresult *res; char *funcsig; /* identity signature */ ! char *funcfullsig; /* full signature */ char *funcsig_tag; char *proretset; char *prosrc; --- 9608,9614 ---- PQExpBuffer asPart; PGresult *res; char *funcsig; /* identity signature */ ! char *funcfullsig = NULL; /* full signature */ char *funcsig_tag; char *proretset; char *prosrc; *************** dumpFunc(Archive *fout, FuncInfo *finfo) *** 9916,9928 **** funcsig = format_function_arguments(finfo, funciargs, false); } else - { /* pre-8.4, do it ourselves */ funcsig = format_function_arguments_old(fout, finfo, nallargs, allargtypes, argmodes, argnames); - funcfullsig = funcsig; - } funcsig_tag = format_function_signature(fout, finfo, false); --- 9916,9925 ---- *************** dumpFunc(Archive *fout, FuncInfo *finfo) *** 9933,9939 **** fmtId(finfo->dobj.namespace->dobj.name), funcsig); ! appendPQExpBuffer(q, "CREATE FUNCTION %s ", funcfullsig); if (funcresult) appendPQExpBuffer(q, "RETURNS %s", funcresult); else --- 9930,9937 ---- fmtId(finfo->dobj.namespace->dobj.name), funcsig); ! appendPQExpBuffer(q, "CREATE FUNCTION %s ", funcfullsig ? funcfullsig : ! funcsig); if (funcresult) appendPQExpBuffer(q, "RETURNS %s", funcresult); else *************** dumpFunc(Archive *fout, FuncInfo *finfo) *** 10056,10061 **** --- 10054,10061 ---- destroyPQExpBuffer(labelq); destroyPQExpBuffer(asPart); free(funcsig); + if (funcfullsig) + free(funcfullsig); free(funcsig_tag); if (allargtypes) free(allargtypes); *************** dumpAgg(Archive *fout, AggInfo *agginfo) *** 11512,11518 **** PQExpBuffer labelq; PQExpBuffer details; char *aggsig; /* identity signature */ ! char *aggfullsig; /* full signature */ char *aggsig_tag; PGresult *res; int i_aggtransfn; --- 11512,11518 ---- PQExpBuffer labelq; PQExpBuffer details; char *aggsig; /* identity signature */ ! char *aggfullsig = NULL; /* full signature */ char *aggsig_tag; PGresult *res; int i_aggtransfn; *************** dumpAgg(Archive *fout, AggInfo *agginfo) *** 11660,11670 **** aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true); } else - { /* pre-8.4, do it ourselves */ aggsig = format_aggregate_signature(agginfo, fout, true); - aggfullsig = aggsig; - } aggsig_tag = format_aggregate_signature(agginfo, fout, false); --- 11660,11667 ---- *************** dumpAgg(Archive *fout, AggInfo *agginfo) *** 11734,11740 **** aggsig); appendPQExpBuffer(q, "CREATE AGGREGATE %s (\n%s\n);\n", ! aggfullsig, details->data); appendPQExpBuffer(labelq, "AGGREGATE %s", aggsig); --- 11731,11737 ---- aggsig); appendPQExpBuffer(q, "CREATE AGGREGATE %s (\n%s\n);\n", ! aggfullsig ? aggfullsig : aggsig, details->data); appendPQExpBuffer(labelq, "AGGREGATE %s", aggsig); *************** dumpAgg(Archive *fout, AggInfo *agginfo) *** 11777,11782 **** --- 11774,11781 ---- agginfo->aggfn.rolname, agginfo->aggfn.proacl); free(aggsig); + if (aggfullsig) + free(aggfullsig); free(aggsig_tag); PQclear(res); --5vNYLRcllDrimb99 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --5vNYLRcllDrimb99--