Skip to content

Commit f4ceb7a

Browse files
committed
Fix regex match failures for backrefs combined with non-greedy quantifiers.
An ancient logic error in cfindloop() could cause the regex engine to fail to find matches that begin later than the start of the string. This function is only used when the regex pattern contains a back reference, and so far as we can tell the error is only reachable if the pattern is non-greedy (i.e. its first quantifier uses the ? modifier). Furthermore, the actual match must begin after some potential match that satisfies the DFA but then fails the back-reference's match test. Reported and fixed by Jeevan Chalke, with cosmetic adjustments by me.
1 parent 2f397a0 commit f4ceb7a

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/backend/regex/regexec.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -467,19 +467,21 @@ cfindloop(struct vars * v,
467467
*coldp = cold;
468468
return er;
469469
}
470-
if ((shorter) ? end == estop : end == begin)
471-
{
472-
/* no point in trying again */
473-
*coldp = cold;
474-
return REG_NOMATCH;
475-
}
476-
/* go around and try again */
470+
/* try next shorter/longer match with same begin point */
477471
if (shorter)
472+
{
473+
if (end == estop)
474+
break; /* NOTE BREAK OUT */
478475
estart = end + 1;
476+
}
479477
else
478+
{
479+
if (end == begin)
480+
break; /* NOTE BREAK OUT */
480481
estop = end - 1;
481-
}
482-
}
482+
}
483+
} /* end loop over endpoint positions */
484+
} /* end loop over beginning positions */
483485
} while (close < v->stop);
484486

485487
*coldp = cold;

0 commit comments

Comments
 (0)