Further tweak memory management for regex DFAs.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 8 Mar 2021 21:32:29 +0000 (16:32 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 8 Mar 2021 21:32:29 +0000 (16:32 -0500)
Coverity is still unhappy after commit 190c79884, and after looking
closer I think it might be onto something.  The callers of newdfa()
typically drop out if v->err has been set nonzero, which newdfa()
is faithfully doing if it fails.  However, what if v->err was already
nonzero before we entered newdfa()?  Then newdfa() could succeed and
the caller would promptly leak its result.

I don't think this scenario can actually happen, but the predicate
"v->err is always zero when newdfa() is called" seems difficult to be
entirely sure of; there's a good deal of code that potentially could
get that wrong.

It seems better to adjust the callers to directly check for a null
result instead of relying on ISERR() tests.  This is slightly cheaper
than the previous coding anyway.

Lacking evidence that there's any real bug, no back-patch.

src/backend/regex/rege_dfa.c
src/backend/regex/regexec.c

index 18f87c69d4754f6a4e17a55b4e7b65875633154d..1d79d734468f94db502ad3db599cfbc5553134cf 100644 (file)
@@ -604,6 +604,8 @@ lastcold(struct vars *v,
 
 /*
  * newdfa - set up a fresh DFA
+ *
+ * Returns NULL (and sets v->err) on failure.
  */
 static struct dfa *
 newdfa(struct vars *v,
index 8d7777f8c62ff4eb0a8a8502a998e155df83b8a4..5b9a0878203224effce404e8e798cfaa37348cc4 100644 (file)
@@ -351,7 +351,7 @@ getsubdfa(struct vars *v,
        if (d == NULL)
        {
                d = newdfa(v, &t->cnfa, &v->g->cmap, DOMALLOC);
-               if (ISERR())
+               if (d == NULL)
                        return NULL;
                /* set up additional info if this is a backref node */
                if (t->op == 'b')
@@ -381,8 +381,6 @@ getladfa(struct vars *v,
                struct subre *sub = &v->g->lacons[n];
 
                v->ladfas[n] = newdfa(v, &sub->cnfa, &v->g->cmap, DOMALLOC);
-               if (ISERR())
-                       return NULL;
                /* a LACON can't contain a backref, so nothing else to do */
        }
        return v->ladfas[n];
@@ -408,8 +406,8 @@ find(struct vars *v,
 
        /* first, a shot with the search RE */
        s = newdfa(v, &v->g->search, cm, &v->dfa1);
-       assert(!(ISERR() && s != NULL));
-       NOERR();
+       if (s == NULL)
+               return v->err;
        MDEBUG(("\nsearch at %ld\n", LOFF(v->start)));
        cold = NULL;
        close = shortest(v, s, v->search_start, v->search_start, v->stop,
@@ -436,8 +434,8 @@ find(struct vars *v,
        cold = NULL;
        MDEBUG(("between %ld and %ld\n", LOFF(open), LOFF(close)));
        d = newdfa(v, cnfa, cm, &v->dfa1);
-       assert(!(ISERR() && d != NULL));
-       NOERR();
+       if (d == NULL)
+               return v->err;
        for (begin = open; begin <= close; begin++)
        {
                MDEBUG(("\nfind trying at %ld\n", LOFF(begin)));
@@ -493,11 +491,11 @@ cfind(struct vars *v,
        int                     ret;
 
        s = newdfa(v, &v->g->search, cm, &v->dfa1);
-       NOERR();
+       if (s == NULL)
+               return v->err;
        d = newdfa(v, cnfa, cm, &v->dfa2);
-       if (ISERR())
+       if (d == NULL)
        {
-               assert(d == NULL);
                freedfa(s);
                return v->err;
        }