Skip to content

Commit 35a173a

Browse files
committed
Fix assorted memory leaks in pg_hba.conf parsing. Over a sufficiently
large number of SIGHUP cycles, these would have run the postmaster out of memory. Noted while testing memory-leak scenario in postgresql.conf configuration-change-printing patch.
1 parent 54d60bb commit 35a173a

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/backend/libpq/hba.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/libpq/hba.c,v 1.191 2009/10/01 01:58:57 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/libpq/hba.c,v 1.192 2009/10/03 20:04:39 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -804,16 +804,12 @@ parse_hba_line(List *line, int line_num, HbaLine *parsedline)
804804
token, gai_strerror(ret)),
805805
errcontext("line %d of configuration file \"%s\"",
806806
line_num, HbaFileName)));
807-
if (cidr_slash)
808-
*cidr_slash = '/';
809807
if (gai_result)
810808
pg_freeaddrinfo_all(hints.ai_family, gai_result);
809+
pfree(token);
811810
return false;
812811
}
813812

814-
if (cidr_slash)
815-
*cidr_slash = '/';
816-
817813
memcpy(&parsedline->addr, gai_result->ai_addr,
818814
gai_result->ai_addrlen);
819815
pg_freeaddrinfo_all(hints.ai_family, gai_result);
@@ -824,18 +820,22 @@ parse_hba_line(List *line, int line_num, HbaLine *parsedline)
824820
if (pg_sockaddr_cidr_mask(&parsedline->mask, cidr_slash + 1,
825821
parsedline->addr.ss_family) < 0)
826822
{
823+
*cidr_slash = '/'; /* restore token for message */
827824
ereport(LOG,
828825
(errcode(ERRCODE_CONFIG_FILE_ERROR),
829826
errmsg("invalid CIDR mask in address \"%s\"",
830827
token),
831828
errcontext("line %d of configuration file \"%s\"",
832829
line_num, HbaFileName)));
830+
pfree(token);
833831
return false;
834832
}
833+
pfree(token);
835834
}
836835
else
837836
{
838837
/* Read the mask field. */
838+
pfree(token);
839839
line_item = lnext(line_item);
840840
if (!line_item)
841841
{
@@ -1266,7 +1266,7 @@ check_hba(hbaPort *port)
12661266
}
12671267

12681268
/*
1269-
* Free the contents of a hba record
1269+
* Free an HbaLine structure
12701270
*/
12711271
static void
12721272
free_hba_record(HbaLine *record)
@@ -1275,6 +1275,8 @@ free_hba_record(HbaLine *record)
12751275
pfree(record->database);
12761276
if (record->role)
12771277
pfree(record->role);
1278+
if (record->usermap)
1279+
pfree(record->usermap);
12781280
if (record->pamservice)
12791281
pfree(record->pamservice);
12801282
if (record->ldapserver)
@@ -1287,6 +1289,7 @@ free_hba_record(HbaLine *record)
12871289
pfree(record->krb_server_hostname);
12881290
if (record->krb_realm)
12891291
pfree(record->krb_realm);
1292+
pfree(record);
12901293
}
12911294

12921295
/*
@@ -1355,20 +1358,22 @@ load_hba(void)
13551358
{
13561359
/* Parse error in the file, so indicate there's a problem */
13571360
free_hba_record(newline);
1358-
pfree(newline);
1361+
ok = false;
13591362

13601363
/*
13611364
* Keep parsing the rest of the file so we can report errors on
13621365
* more than the first row. Error has already been reported in the
13631366
* parsing function, so no need to log it here.
13641367
*/
1365-
ok = false;
13661368
continue;
13671369
}
13681370

13691371
new_parsed_lines = lappend(new_parsed_lines, newline);
13701372
}
13711373

1374+
/* Free the temporary lists */
1375+
free_lines(&hba_lines, &hba_line_nums);
1376+
13721377
if (!ok)
13731378
{
13741379
/* Parsing failed at one or more rows, so bail out */
@@ -1380,9 +1385,6 @@ load_hba(void)
13801385
clean_hba_list(parsed_hba_lines);
13811386
parsed_hba_lines = new_parsed_lines;
13821387

1383-
/* Free the temporary lists */
1384-
free_lines(&hba_lines, &hba_line_nums);
1385-
13861388
return true;
13871389
}
13881390

0 commit comments

Comments
 (0)