Fix some possible low-memory failures in regexp compilation.
authorTom Lane <[email protected]>
Wed, 12 Aug 2015 04:48:11 +0000 (00:48 -0400)
committerTom Lane <[email protected]>
Wed, 12 Aug 2015 04:48:44 +0000 (00:48 -0400)
newnfa() failed to set the regex error state when malloc() fails.
Several places in regcomp.c failed to check for an error after calling
subre().  Each of these mistakes could lead to null-pointer-dereference
crashes in memory-starved backends.

Report and patch by Andreas Seltenreich.  Back-patch to all branches.

src/backend/regex/regc_nfa.c
src/backend/regex/regcomp.c

index 776bb59b867e9fd981164a69713c3d8959a944e9..e0d5af205f673cf96da8c78a392c9b43d0626ac3 100644 (file)
@@ -52,7 +52,10 @@ newnfa(struct vars * v,
 
    nfa = (struct nfa *) MALLOC(sizeof(struct nfa));
    if (nfa == NULL)
+   {
+       ERR(REG_ESPACE);
        return NULL;
+   }
 
    nfa->states = NULL;
    nfa->slast = NULL;
index 742539a90b968063de0f897e3ede1e07af0d7c08..38f48d5ddb818229674b1653e662f4b8b51fe6d2 100644 (file)
@@ -942,6 +942,7 @@ parseqatom(struct vars * v,
            NOERR();
            assert(v->nextvalue > 0);
            atom = subre(v, 'b', BACKR, lp, rp);
+           NOERR();
            subno = v->nextvalue;
            atom->subno = subno;
            EMPTYARC(lp, rp);   /* temporarily, so there's something */
@@ -1076,6 +1077,7 @@ parseqatom(struct vars * v,
 
    /* break remaining subRE into x{...} and what follows */
    t = subre(v, '.', COMBINE(qprefer, atom->flags), lp, rp);
+   NOERR();
    t->left = atom;
    atomp = &t->left;
 
@@ -1084,6 +1086,7 @@ parseqatom(struct vars * v,
    /* split top into prefix and remaining */
    assert(top->op == '=' && top->left == NULL && top->right == NULL);
    top->left = subre(v, '=', top->flags, top->begin, lp);
+   NOERR();
    top->op = '.';
    top->right = t;