From: Heikki Linnakangas Date: Thu, 2 Jul 2015 17:58:51 +0000 (+0300) Subject: Plug some trivial memory leaks in pg_dump and pg_upgrade. X-Git-Tag: REL9_6_BETA1~1745 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f712289ffad7c3fb6eb3be4f81adb0aa0981c9f7;p=postgresql Plug some trivial memory leaks in pg_dump and pg_upgrade. There's no point in trying to free every small allocation in these programs that are used in a one-shot fashion, but these ones seems like an improvement on readability grounds. Michael Paquier, per Coverity report. --- diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 32ac26f1db..b5c529bb8e 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -10593,6 +10593,8 @@ dumpCast(Archive *fout, DumpOptions *dopt, CastInfo *cast) PQExpBuffer delqry; PQExpBuffer labelq; FuncInfo *funcInfo = NULL; + char *sourceType; + char *targetType; /* Skip if not to be dumped */ if (!cast->dobj.dump || dopt->dataOnly) @@ -10616,13 +10618,13 @@ dumpCast(Archive *fout, DumpOptions *dopt, CastInfo *cast) delqry = createPQExpBuffer(); labelq = createPQExpBuffer(); + sourceType = getFormattedTypeName(fout, cast->castsource, zeroAsNone); + targetType = getFormattedTypeName(fout, cast->casttarget, zeroAsNone); appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n", - getFormattedTypeName(fout, cast->castsource, zeroAsNone), - getFormattedTypeName(fout, cast->casttarget, zeroAsNone)); + sourceType, targetType); appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ", - getFormattedTypeName(fout, cast->castsource, zeroAsNone), - getFormattedTypeName(fout, cast->casttarget, zeroAsNone)); + sourceType, targetType); switch (cast->castmethod) { @@ -10660,8 +10662,7 @@ dumpCast(Archive *fout, DumpOptions *dopt, CastInfo *cast) appendPQExpBufferStr(defqry, ";\n"); appendPQExpBuffer(labelq, "CAST (%s AS %s)", - getFormattedTypeName(fout, cast->castsource, zeroAsNone), - getFormattedTypeName(fout, cast->casttarget, zeroAsNone)); + sourceType, targetType); if (dopt->binary_upgrade) binary_upgrade_extension_member(defqry, &cast->dobj, labelq->data); @@ -10679,6 +10680,9 @@ dumpCast(Archive *fout, DumpOptions *dopt, CastInfo *cast) NULL, "", cast->dobj.catId, 0, cast->dobj.dumpId); + free(sourceType); + free(targetType); + destroyPQExpBuffer(defqry); destroyPQExpBuffer(delqry); destroyPQExpBuffer(labelq); @@ -10696,6 +10700,7 @@ dumpTransform(Archive *fout, DumpOptions *dopt, TransformInfo *transform) FuncInfo *fromsqlFuncInfo = NULL; FuncInfo *tosqlFuncInfo = NULL; char *lanname; + char *transformType; /* Skip if not to be dumped */ if (!transform->dobj.dump || dopt->dataOnly) @@ -10723,14 +10728,13 @@ dumpTransform(Archive *fout, DumpOptions *dopt, TransformInfo *transform) labelq = createPQExpBuffer(); lanname = get_language_name(fout, transform->trflang); + transformType = getFormattedTypeName(fout, transform->trftype, zeroAsNone); appendPQExpBuffer(delqry, "DROP TRANSFORM FOR %s LANGUAGE %s;\n", - getFormattedTypeName(fout, transform->trftype, zeroAsNone), - lanname); + transformType, lanname); appendPQExpBuffer(defqry, "CREATE TRANSFORM FOR %s LANGUAGE %s (", - getFormattedTypeName(fout, transform->trftype, zeroAsNone), - lanname); + transformType, lanname); if (!transform->trffromsql && !transform->trftosql) write_msg(NULL, "WARNING: bogus transform definition, at least one of trffromsql and trftosql should be nonzero\n"); @@ -10777,8 +10781,7 @@ dumpTransform(Archive *fout, DumpOptions *dopt, TransformInfo *transform) appendPQExpBuffer(defqry, ");\n"); appendPQExpBuffer(labelq, "TRANSFORM FOR %s LANGUAGE %s", - getFormattedTypeName(fout, transform->trftype, zeroAsNone), - lanname); + transformType, lanname); if (dopt->binary_upgrade) binary_upgrade_extension_member(defqry, &transform->dobj, labelq->data); @@ -10797,6 +10800,7 @@ dumpTransform(Archive *fout, DumpOptions *dopt, TransformInfo *transform) transform->dobj.catId, 0, transform->dobj.dumpId); free(lanname); + free(transformType); destroyPQExpBuffer(defqry); destroyPQExpBuffer(delqry); destroyPQExpBuffer(labelq); diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c index d98c83e099..c4b6ae85aa 100644 --- a/src/bin/pg_dump/pg_dumpall.c +++ b/src/bin/pg_dump/pg_dumpall.c @@ -1442,6 +1442,13 @@ dumpCreateDB(PGconn *conn) free(fdbname); } + if (default_encoding) + free(default_encoding); + if (default_collate) + free(default_collate); + if (default_ctype) + free(default_ctype); + PQclear(res); destroyPQExpBuffer(buf); diff --git a/src/bin/pg_upgrade/check.c b/src/bin/pg_upgrade/check.c index 5a91871c35..41d4606dd5 100644 --- a/src/bin/pg_upgrade/check.c +++ b/src/bin/pg_upgrade/check.c @@ -337,8 +337,14 @@ equivalent_locale(int category, const char *loca, const char *locb) lenb = charb ? (charb - canonb) : strlen(canonb); if (lena == lenb && pg_strncasecmp(canona, canonb, lena) == 0) + { + pg_free(canona); + pg_free(canonb); return true; + } + pg_free(canona); + pg_free(canonb); return false; }