Skip to content

Commit 54fd196

Browse files
committed
Prevent corner-case core dump in rfree().
rfree() failed to cope with the case that pg_regcomp() had initialized the regex_t struct but then failed to allocate any memory for re->re_guts (ie, the first malloc call in pg_regcomp() failed). It would try to touch the guts struct anyway, and thus dump core. This is a sufficiently narrow corner case that it's not surprising it's never been seen in the field; but still a bug is a bug, so patch all active branches. Noted while investigating whether we need to call pg_regfree after a failure return from pg_regcomp. Other than this bug, it turns out we don't, so adjust comments appropriately.
1 parent 2686da9 commit 54fd196

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/backend/regex/regcomp.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ static struct fns functions = {
278278

279279
/*
280280
* pg_regcomp - compile regular expression
281+
*
282+
* Note: on failure, no resources remain allocated, so pg_regfree()
283+
* need not be applied to re.
281284
*/
282285
int
283286
pg_regcomp(regex_t *re,
@@ -1870,15 +1873,18 @@ rfree(regex_t *re)
18701873
g = (struct guts *) re->re_guts;
18711874
re->re_guts = NULL;
18721875
re->re_fns = NULL;
1873-
g->magic = 0;
1874-
freecm(&g->cmap);
1875-
if (g->tree != NULL)
1876-
freesubre((struct vars *) NULL, g->tree);
1877-
if (g->lacons != NULL)
1878-
freelacons(g->lacons, g->nlacons);
1879-
if (!NULLCNFA(g->search))
1880-
freecnfa(&g->search);
1881-
FREE(g);
1876+
if (g != NULL)
1877+
{
1878+
g->magic = 0;
1879+
freecm(&g->cmap);
1880+
if (g->tree != NULL)
1881+
freesubre((struct vars *) NULL, g->tree);
1882+
if (g->lacons != NULL)
1883+
freelacons(g->lacons, g->nlacons);
1884+
if (!NULLCNFA(g->search))
1885+
freecnfa(&g->search);
1886+
FREE(g);
1887+
}
18821888
}
18831889

18841890
#ifdef REG_DEBUG

src/backend/utils/adt/regexp.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,8 @@ RE_compile_and_cache(text *text_re, int cflags, Oid collation)
187187

188188
if (regcomp_result != REG_OKAY)
189189
{
190-
/* re didn't compile */
190+
/* re didn't compile (no need for pg_regfree, if so) */
191191
pg_regerror(regcomp_result, &re_temp.cre_re, errMsg, sizeof(errMsg));
192-
/* XXX should we pg_regfree here? */
193192
ereport(ERROR,
194193
(errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
195194
errmsg("invalid regular expression: %s", errMsg)));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy