Skip to content

Commit d37e0d0

Browse files
committed
Release PQconninfoOptions array in GetDbnameFromConnectionOptions().
It wasn't getting freed in one code path, which Coverity identified as a resource leak. It's probably of little consequence, but re-ordering the code into the correct sequence is no more work than dismissing the complaint. Minor oversight in commit a145f42. While here, improve the unreasonably clunky coding of FindDbnameInConnParams: use of an output parameter is unnecessary and prone to uninitialized-variable problems.
1 parent 225e1dd commit d37e0d0

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/bin/pg_basebackup/streamutil.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
int WalSegSz;
3535

3636
static bool RetrieveDataDirCreatePerm(PGconn *conn);
37-
static void FindDbnameInConnParams(PQconninfoOption *conn_opts, char **dbname);
37+
static char *FindDbnameInConnParams(PQconninfoOption *conn_opts);
3838

3939
/* SHOW command for replication connection was introduced in version 10 */
4040
#define MINIMUM_VERSION_FOR_SHOW_CMD 100000
@@ -272,21 +272,21 @@ GetConnection(void)
272272
* FindDbnameInConnParams
273273
*
274274
* This is a helper function for GetDbnameFromConnectionOptions(). Extract
275-
* the value of dbname from PQconninfoOption parameters.
275+
* the value of dbname from PQconninfoOption parameters, if it's present.
276+
* Returns a strdup'd result or NULL.
276277
*/
277-
static void
278-
FindDbnameInConnParams(PQconninfoOption *conn_opts, char **dbname)
278+
static char *
279+
FindDbnameInConnParams(PQconninfoOption *conn_opts)
279280
{
280281
PQconninfoOption *conn_opt;
281282

282-
Assert(dbname != NULL);
283-
284283
for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
285284
{
286-
if ((strcmp(conn_opt->keyword, "dbname") == 0) &&
285+
if (strcmp(conn_opt->keyword, "dbname") == 0 &&
287286
conn_opt->val != NULL && conn_opt->val[0] != '\0')
288-
*dbname = pg_strdup(conn_opt->val);
287+
return pg_strdup(conn_opt->val);
289288
}
289+
return NULL;
290290
}
291291

292292
/*
@@ -304,9 +304,9 @@ FindDbnameInConnParams(PQconninfoOption *conn_opts, char **dbname)
304304
char *
305305
GetDbnameFromConnectionOptions(void)
306306
{
307-
PQconninfoOption *conn_opts = NULL;
307+
PQconninfoOption *conn_opts;
308308
char *err_msg = NULL;
309-
char *dbname = NULL;
309+
char *dbname;
310310

311311
/* First try to get the dbname from connection string. */
312312
if (connection_string)
@@ -315,12 +315,11 @@ GetDbnameFromConnectionOptions(void)
315315
if (conn_opts == NULL)
316316
pg_fatal("%s", err_msg);
317317

318-
FindDbnameInConnParams(conn_opts, &dbname);
318+
dbname = FindDbnameInConnParams(conn_opts);
319+
320+
PQconninfoFree(conn_opts);
319321
if (dbname)
320-
{
321-
PQconninfoFree(conn_opts);
322322
return dbname;
323-
}
324323
}
325324

326325
/*
@@ -331,7 +330,7 @@ GetDbnameFromConnectionOptions(void)
331330
if (conn_opts == NULL)
332331
pg_fatal("out of memory");
333332

334-
FindDbnameInConnParams(conn_opts, &dbname);
333+
dbname = FindDbnameInConnParams(conn_opts);
335334

336335
PQconninfoFree(conn_opts);
337336
return dbname;

0 commit comments

Comments
 (0)