Skip to content

Commit da7d81d

Browse files
committed
Avoid useless malloc/free traffic around getFormattedTypeName().
Coverity complained that one caller of getFormattedTypeName() failed to free the returned string. Which is true, but rather than fixing that one, let's get rid of this tedious and error-prone requirement. Now that getFormattedTypeName() caches its result, strdup'ing that result and expecting the caller to free it accomplishes little except to waste cycles. We do create a leak in the case where getTypes didn't make a TypeInfo for the type, but that basically shouldn't ever happen. Back-patch, as commit 6c450a8 was. This isn't a particularly interesting bug fix, but the API change seems like a hazard for future back-patching activity if we don't back-patch it.
1 parent cbba6ba commit da7d81d

File tree

1 file changed

+29
-45
lines changed

1 file changed

+29
-45
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ static char *convertRegProcReference(Archive *fout,
255255
static char *getFormattedOperatorName(Archive *fout, const char *oproid);
256256
static char *convertTSFunction(Archive *fout, Oid funcOid);
257257
static Oid findLastBuiltinOid_V71(Archive *fout);
258-
static char *getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts);
258+
static const char *getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts);
259259
static void getBlobs(Archive *fout);
260260
static void dumpBlob(Archive *fout, BlobInfo *binfo);
261261
static int dumpBlobs(Archive *fout, void *arg);
@@ -11009,13 +11009,9 @@ dumpBaseType(Archive *fout, TypeInfo *tyinfo)
1100911009
}
1101011010

1101111011
if (OidIsValid(tyinfo->typelem))
11012-
{
11013-
char *elemType;
11014-
11015-
elemType = getFormattedTypeName(fout, tyinfo->typelem, zeroIsError);
11016-
appendPQExpBuffer(q, ",\n ELEMENT = %s", elemType);
11017-
free(elemType);
11018-
}
11012+
appendPQExpBuffer(q, ",\n ELEMENT = %s",
11013+
getFormattedTypeName(fout, tyinfo->typelem,
11014+
zeroIsError));
1101911015

1102011016
if (strcmp(typcategory, "U") != 0)
1102111017
{
@@ -11818,7 +11814,7 @@ format_function_arguments_old(Archive *fout,
1181811814
for (j = 0; j < nallargs; j++)
1181911815
{
1182011816
Oid typid;
11821-
char *typname;
11817+
const char *typname;
1182211818
const char *argmode;
1182311819
const char *argname;
1182411820

@@ -11857,7 +11853,6 @@ format_function_arguments_old(Archive *fout,
1185711853
argname ? fmtId(argname) : "",
1185811854
argname ? " " : "",
1185911855
typname);
11860-
free(typname);
1186111856
}
1186211857
appendPQExpBufferChar(&fn, ')');
1186311858
return fn.data;
@@ -11886,15 +11881,12 @@ format_function_signature(Archive *fout, FuncInfo *finfo, bool honor_quotes)
1188611881
appendPQExpBuffer(&fn, "%s(", finfo->dobj.name);
1188711882
for (j = 0; j < finfo->nargs; j++)
1188811883
{
11889-
char *typname;
11890-
1189111884
if (j > 0)
1189211885
appendPQExpBufferStr(&fn, ", ");
1189311886

11894-
typname = getFormattedTypeName(fout, finfo->argtypes[j],
11895-
zeroIsError);
11896-
appendPQExpBufferStr(&fn, typname);
11897-
free(typname);
11887+
appendPQExpBufferStr(&fn,
11888+
getFormattedTypeName(fout, finfo->argtypes[j],
11889+
zeroIsError));
1189811890
}
1189911891
appendPQExpBufferChar(&fn, ')');
1190011892
return fn.data;
@@ -11938,7 +11930,6 @@ dumpFunc(Archive *fout, FuncInfo *finfo)
1193811930
char *prosupport;
1193911931
char *proparallel;
1194011932
char *lanname;
11941-
char *rettypename;
1194211933
int nallargs;
1194311934
char **allargtypes = NULL;
1194411935
char **argmodes = NULL;
@@ -12295,14 +12286,10 @@ dumpFunc(Archive *fout, FuncInfo *finfo)
1229512286
else if (funcresult)
1229612287
appendPQExpBuffer(q, " RETURNS %s", funcresult);
1229712288
else
12298-
{
12299-
rettypename = getFormattedTypeName(fout, finfo->prorettype,
12300-
zeroIsError);
1230112289
appendPQExpBuffer(q, " RETURNS %s%s",
1230212290
(proretset[0] == 't') ? "SETOF " : "",
12303-
rettypename);
12304-
free(rettypename);
12305-
}
12291+
getFormattedTypeName(fout, finfo->prorettype,
12292+
zeroIsError));
1230612293

1230712294
appendPQExpBuffer(q, "\n LANGUAGE %s", fmtId(lanname));
1230812295

@@ -12509,8 +12496,8 @@ dumpCast(Archive *fout, CastInfo *cast)
1250912496
PQExpBuffer labelq;
1251012497
PQExpBuffer castargs;
1251112498
FuncInfo *funcInfo = NULL;
12512-
char *sourceType;
12513-
char *targetType;
12499+
const char *sourceType;
12500+
const char *targetType;
1251412501

1251512502
/* Skip if not to be dumped */
1251612503
if (!cast->dobj.dump || dopt->dataOnly)
@@ -12596,9 +12583,6 @@ dumpCast(Archive *fout, CastInfo *cast)
1259612583
NULL, "",
1259712584
cast->dobj.catId, 0, cast->dobj.dumpId);
1259812585

12599-
free(sourceType);
12600-
free(targetType);
12601-
1260212586
destroyPQExpBuffer(defqry);
1260312587
destroyPQExpBuffer(delqry);
1260412588
destroyPQExpBuffer(labelq);
@@ -12619,7 +12603,7 @@ dumpTransform(Archive *fout, TransformInfo *transform)
1261912603
FuncInfo *fromsqlFuncInfo = NULL;
1262012604
FuncInfo *tosqlFuncInfo = NULL;
1262112605
char *lanname;
12622-
char *transformType;
12606+
const char *transformType;
1262312607

1262412608
/* Skip if not to be dumped */
1262512609
if (!transform->dobj.dump || dopt->dataOnly)
@@ -12726,7 +12710,6 @@ dumpTransform(Archive *fout, TransformInfo *transform)
1272612710
transform->dobj.catId, 0, transform->dobj.dumpId);
1272712711

1272812712
free(lanname);
12729-
free(transformType);
1273012713
destroyPQExpBuffer(defqry);
1273112714
destroyPQExpBuffer(delqry);
1273212715
destroyPQExpBuffer(labelq);
@@ -14018,17 +14001,11 @@ format_aggregate_signature(AggInfo *agginfo, Archive *fout, bool honor_quotes)
1401814001
{
1401914002
appendPQExpBufferChar(&buf, '(');
1402014003
for (j = 0; j < agginfo->aggfn.nargs; j++)
14021-
{
14022-
char *typname;
14023-
14024-
typname = getFormattedTypeName(fout, agginfo->aggfn.argtypes[j],
14025-
zeroIsError);
14026-
1402714004
appendPQExpBuffer(&buf, "%s%s",
1402814005
(j > 0) ? ", " : "",
14029-
typname);
14030-
free(typname);
14031-
}
14006+
getFormattedTypeName(fout,
14007+
agginfo->aggfn.argtypes[j],
14008+
zeroIsError));
1403214009
appendPQExpBufferChar(&buf, ')');
1403314010
}
1403414011
return buf.data;
@@ -18728,8 +18705,10 @@ findDumpableDependencies(ArchiveHandle *AH, DumpableObject *dobj,
1872818705
*
1872918706
* This does not guarantee to schema-qualify the output, so it should not
1873018707
* be used to create the target object name for CREATE or ALTER commands.
18708+
*
18709+
* Note that the result is cached and must not be freed by the caller.
1873118710
*/
18732-
static char *
18711+
static const char *
1873318712
getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
1873418713
{
1873518714
TypeInfo *typeInfo;
@@ -18740,15 +18719,15 @@ getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
1874018719
if (oid == 0)
1874118720
{
1874218721
if ((opts & zeroAsStar) != 0)
18743-
return pg_strdup("*");
18722+
return "*";
1874418723
else if ((opts & zeroAsNone) != 0)
18745-
return pg_strdup("NONE");
18724+
return "NONE";
1874618725
}
1874718726

1874818727
/* see if we have the result cached in the type's TypeInfo record */
1874918728
typeInfo = findTypeByOid(oid);
1875018729
if (typeInfo && typeInfo->ftypname)
18751-
return pg_strdup(typeInfo->ftypname);
18730+
return typeInfo->ftypname;
1875218731

1875318732
query = createPQExpBuffer();
1875418733
appendPQExpBuffer(query, "SELECT pg_catalog.format_type('%u'::pg_catalog.oid, NULL)",
@@ -18762,9 +18741,14 @@ getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
1876218741
PQclear(res);
1876318742
destroyPQExpBuffer(query);
1876418743

18765-
/* cache a copy for later requests */
18744+
/*
18745+
* Cache the result for re-use in later requests, if possible. If we
18746+
* don't have a TypeInfo for the type, the string will be leaked once the
18747+
* caller is done with it ... but that case really should not happen, so
18748+
* leaking if it does seems acceptable.
18749+
*/
1876618750
if (typeInfo)
18767-
typeInfo->ftypname = pg_strdup(result);
18751+
typeInfo->ftypname = result;
1876818752

1876918753
return result;
1877018754
}

0 commit comments

Comments
 (0)