Skip to content

Commit

Permalink
Avoid clear whole 4K regex NFA, see https://sourceforge.net/p/scintil…
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed Nov 17, 2023
1 parent 4eae641 commit 5d0801f
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions scintilla/src/RESearch.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ constexpr int isinset(const char *ap, unsigned char c) noexcept {
return ap[c >> 3] & (1 << (c & BITIND));
}

constexpr char GetOpCode(const char *sp, const char *mp) noexcept {
return (sp < mp) ? *sp : END;
}

}

/**
Expand Down Expand Up @@ -435,7 +439,6 @@ const char *RESearch::Compile(const char *pattern, size_t length, FindOption fla
}

const char *RESearch::DoCompile(const char *pattern, size_t length, FindOption flags) noexcept {
memset(nfa, '\0', sizeof(nfa));
memset(bittab, 0, BITBLK);
const bool caseSensitive = FlagSet(flags, FindOption::MatchCase);
const bool posix = FlagSet(flags, FindOption::Posix);
Expand Down Expand Up @@ -590,13 +593,14 @@ const char *RESearch::DoCompile(const char *pattern, size_t length, FindOption f

case '*': /* match 0 or more... */
case '+': /* match 1 or more... */
case '?':
case '?': {
if (p == pattern)
return badpat("Empty closure");
lp = sp; /* previous opcode */
if (*lp == CLO || *lp == LCLO) /* equivalence... */
const char opcode = GetOpCode(sp, mp);
if (opcode == CLO || opcode == LCLO) /* equivalence... */
break;
switch (*lp) {
switch (opcode) {

case BOL:
case BOT:
Expand Down Expand Up @@ -626,7 +630,7 @@ const char *RESearch::DoCompile(const char *pattern, size_t length, FindOption f
else *mp = CLO;

mp = sp;
break;
} break;

case '\\': /* tags, backrefs... */
i++;
Expand All @@ -635,7 +639,7 @@ const char *RESearch::DoCompile(const char *pattern, size_t length, FindOption f
*mp++ = BOW;
break;
case '>':
if (*sp == BOW)
if (GetOpCode(sp, mp) == BOW)
return badpat("Null pattern inside \\<\\>");
*mp++ = EOW;
break;
Expand Down Expand Up @@ -668,7 +672,7 @@ const char *RESearch::DoCompile(const char *pattern, size_t length, FindOption f
return badpat("Too many \\(\\) pairs");
}
} else if (!posix && *p == ')') {
if (*sp == BOT)
if (GetOpCode(sp, mp) == BOT)
return badpat("Null pattern inside \\(\\)");
if (tagi > 0) {
*mp++ = EOT;
Expand Down Expand Up @@ -704,7 +708,7 @@ const char *RESearch::DoCompile(const char *pattern, size_t length, FindOption f
return badpat("Too many () pairs");
}
} else if (posix && *p == ')') {
if (*sp == BOT)
if (GetOpCode(sp, mp) == BOT)
return badpat("Null pattern inside ()");
if (tagi > 0) {
*mp++ = EOT;
Expand Down

0 comments on commit 5d0801f

Please sign in to comment.