Skip to content

Commit b8c24f7

Browse files
committed
Fix two low-probability memory leaks in regular expression parsing.
If pg_regcomp failed after having invoked markst/cleanst, it would leak any "struct subre" nodes it had created. (We've already detected all regex syntax errors at that point, so the only likely causes of later failure would be query cancel or out-of-memory.) To fix, make sure freesrnode knows the difference between the pre-cleanst and post-cleanst cleanup procedures. Add some documentation of this less-than-obvious point. Also, newlacon did the wrong thing with an out-of-memory failure from realloc(), so that the previously allocated array would be leaked. Both of these are pretty low-probability scenarios, but a bug is a bug, so patch all the way back. Per bug #10976 from Arthur O'Dwyer.
1 parent bf08864 commit b8c24f7

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/backend/regex/regcomp.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,8 +1656,9 @@ freesrnode(struct vars * v, /* might be NULL */
16561656
freecnfa(&sr->cnfa);
16571657
sr->flags = 0;
16581658

1659-
if (v != NULL)
1659+
if (v != NULL && v->treechain != NULL)
16601660
{
1661+
/* we're still parsing, maybe we can reuse the subre */
16611662
sr->left = v->treefree;
16621663
v->treefree = sr;
16631664
}
@@ -1703,6 +1704,20 @@ numst(struct subre * t,
17031704

17041705
/*
17051706
* markst - mark tree nodes as INUSE
1707+
*
1708+
* Note: this is a great deal more subtle than it looks. During initial
1709+
* parsing of a regex, all subres are linked into the treechain list;
1710+
* discarded ones are also linked into the treefree list for possible reuse.
1711+
* After we are done creating all subres required for a regex, we run markst()
1712+
* then cleanst(), which results in discarding all subres not reachable from
1713+
* v->tree. We then clear v->treechain, indicating that subres must be found
1714+
* by descending from v->tree. This changes the behavior of freesubre(): it
1715+
* will henceforth FREE() unwanted subres rather than sticking them into the
1716+
* treefree list. (Doing that any earlier would result in dangling links in
1717+
* the treechain list.) This all means that freev() will clean up correctly
1718+
* if invoked before or after markst()+cleanst(); but it would not work if
1719+
* called partway through this state conversion, so we mustn't error out
1720+
* in or between these two functions.
17061721
*/
17071722
static void
17081723
markst(struct subre * t)
@@ -1800,25 +1815,27 @@ newlacon(struct vars * v,
18001815
int pos)
18011816
{
18021817
int n;
1818+
struct subre *newlacons;
18031819
struct subre *sub;
18041820

18051821
if (v->nlacons == 0)
18061822
{
1807-
v->lacons = (struct subre *) MALLOC(2 * sizeof(struct subre));
18081823
n = 1; /* skip 0th */
1809-
v->nlacons = 2;
1824+
newlacons = (struct subre *) MALLOC(2 * sizeof(struct subre));
18101825
}
18111826
else
18121827
{
1813-
v->lacons = (struct subre *) REALLOC(v->lacons,
1814-
(v->nlacons + 1) * sizeof(struct subre));
1815-
n = v->nlacons++;
1828+
n = v->nlacons;
1829+
newlacons = (struct subre *) REALLOC(v->lacons,
1830+
(n + 1) * sizeof(struct subre));
18161831
}
1817-
if (v->lacons == NULL)
1832+
if (newlacons == NULL)
18181833
{
18191834
ERR(REG_ESPACE);
18201835
return 0;
18211836
}
1837+
v->lacons = newlacons;
1838+
v->nlacons = n + 1;
18221839
sub = &v->lacons[n];
18231840
sub->begin = begin;
18241841
sub->end = end;

0 commit comments

Comments
 (0)