aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2012-07-15 13:28:09 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2012-07-15 13:28:09 -0400
commitd6270e2df9f5fc8803d8171e0aad98a55257f921 (patch)
tree79e842d05143d144a7a7b7e6be64ce55a45249a0
parentd066cc548de7f21e84aeb27bd43388ba82534759 (diff)
downloadpostgresql-d6270e2df9f5fc8803d8171e0aad98a55257f921.tar.gz
postgresql-d6270e2df9f5fc8803d8171e0aad98a55257f921.zip
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.
-rw-r--r--src/backend/regex/regcomp.c24
-rw-r--r--src/backend/utils/adt/regexp.c3
2 files changed, 16 insertions, 11 deletions
diff --git a/src/backend/regex/regcomp.c b/src/backend/regex/regcomp.c
index 3f7d57c163a..34c633a4e88 100644
--- a/src/backend/regex/regcomp.c
+++ b/src/backend/regex/regcomp.c
@@ -278,6 +278,9 @@ static struct fns functions = {
/*
* pg_regcomp - compile regular expression
+ *
+ * Note: on failure, no resources remain allocated, so pg_regfree()
+ * need not be applied to re.
*/
int
pg_regcomp(regex_t *re,
@@ -1853,15 +1856,18 @@ rfree(regex_t *re)
g = (struct guts *) re->re_guts;
re->re_guts = NULL;
re->re_fns = NULL;
- g->magic = 0;
- freecm(&g->cmap);
- if (g->tree != NULL)
- freesubre((struct vars *) NULL, g->tree);
- if (g->lacons != NULL)
- freelacons(g->lacons, g->nlacons);
- if (!NULLCNFA(g->search))
- freecnfa(&g->search);
- FREE(g);
+ if (g != NULL)
+ {
+ g->magic = 0;
+ freecm(&g->cmap);
+ if (g->tree != NULL)
+ freesubre((struct vars *) NULL, g->tree);
+ if (g->lacons != NULL)
+ freelacons(g->lacons, g->nlacons);
+ if (!NULLCNFA(g->search))
+ freecnfa(&g->search);
+ FREE(g);
+ }
}
#ifdef REG_DEBUG
diff --git a/src/backend/utils/adt/regexp.c b/src/backend/utils/adt/regexp.c
index c590f7e950e..67e205d0816 100644
--- a/src/backend/utils/adt/regexp.c
+++ b/src/backend/utils/adt/regexp.c
@@ -187,9 +187,8 @@ RE_compile_and_cache(text *text_re, int cflags, Oid collation)
if (regcomp_result != REG_OKAY)
{
- /* re didn't compile */
+ /* re didn't compile (no need for pg_regfree, if so) */
pg_regerror(regcomp_result, &re_temp.cre_re, errMsg, sizeof(errMsg));
- /* XXX should we pg_regfree here? */
ereport(ERROR,
(errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
errmsg("invalid regular expression: %s", errMsg)));