Skip to content

Commit 7e23b63

Browse files
committed
Fix some possible low-memory failures in regexp compilation.
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.
1 parent 298d1f8 commit 7e23b63

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

src/backend/regex/regc_nfa.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ newnfa(struct vars * v,
5252

5353
nfa = (struct nfa *) MALLOC(sizeof(struct nfa));
5454
if (nfa == NULL)
55+
{
56+
ERR(REG_ESPACE);
5557
return NULL;
58+
}
5659

5760
nfa->states = NULL;
5861
nfa->slast = NULL;

src/backend/regex/regcomp.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,7 @@ parseqatom(struct vars * v,
934934
NOERR();
935935
assert(v->nextvalue > 0);
936936
atom = subre(v, 'b', BACKR, lp, rp);
937+
NOERR();
937938
subno = v->nextvalue;
938939
atom->subno = subno;
939940
EMPTYARC(lp, rp); /* temporarily, so there's something */
@@ -1064,6 +1065,7 @@ parseqatom(struct vars * v,
10641065

10651066
/* break remaining subRE into x{...} and what follows */
10661067
t = subre(v, '.', COMBINE(qprefer, atom->flags), lp, rp);
1068+
NOERR();
10671069
t->left = atom;
10681070
atomp = &t->left;
10691071

@@ -1072,6 +1074,7 @@ parseqatom(struct vars * v,
10721074
/* split top into prefix and remaining */
10731075
assert(top->op == '=' && top->left == NULL && top->right == NULL);
10741076
top->left = subre(v, '=', top->flags, top->begin, lp);
1077+
NOERR();
10751078
top->op = '.';
10761079
top->right = t;
10771080

0 commit comments

Comments
 (0)