Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions std/regex/internal/parser.d
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,8 @@ if (isForwardRange!R && is(ElementType!R : dchar))
state = State.Start;
break;
default:
if (current >= privateUseStart && current <= privateUseEnd)
enforce(false, "no matching ']' found while parsing character class");
enforce(false, "invalid escape sequence");
}
break;
Expand Down Expand Up @@ -1256,8 +1258,17 @@ if (isForwardRange!R && is(ElementType!R : dchar))
end = parseUniHex(pat, 8);
break;
default:
if (current >= privateUseStart && current <= privateUseEnd)
enforce(false, "no matching ']' found while parsing character class");
error("invalid escape sequence");
}
// Lookahead to check if it's a \T
// where T is sub-pattern terminator in multi-pattern scheme
if (end == '\\' && !pat.empty)
{
if (pat.front >= privateUseStart && pat.front <= privateUseEnd)
enforce(false, "invalid escape sequence");
}
enforce(last <= end,"inverted range");
set.add(last, end + 1);
state = State.Start;
Expand Down Expand Up @@ -1364,8 +1375,7 @@ if (isForwardRange!R && is(ElementType!R : dchar))
"character class syntax error");
enforce(!opstack.empty, "unmatched ']'");
opstack.pop();
next();
if (opstack.empty)
if (!next() || opstack.empty)
break L_CharsetLoop;
auto pair = parseCharTerm();
if (!pair[0].empty)//not only operator e.g. -- or ~~
Expand All @@ -1390,10 +1400,13 @@ if (isForwardRange!R && is(ElementType!R : dchar))
}
vstack.push(pair[0]);
}

}while (!empty || !opstack.empty);
while (!opstack.empty)
apply(opstack.pop(),vstack);
{
enforce(opstack.top != Operator.Open,
"no matching ']' found while parsing character class");
apply(opstack.pop(), vstack);
}
assert(vstack.length == 1);
g.charsetToIr(vstack.top);
}
Expand Down Expand Up @@ -1483,6 +1496,13 @@ if (isForwardRange!R && is(ElementType!R : dchar))
g.markBackref(nref);
break;
default:
// Lookahead to check if it's a \T
// where T is sub-pattern terminator in multi-pattern scheme
if (current == '\\' && !pat.empty)
{
if (pat.front >= privateUseStart && current <= privateUseEnd)
enforce(false, "invalid escape sequence");
}
if (current >= privateUseStart && current <= privateUseEnd)
{
g.endPattern(current - privateUseStart + 1);
Expand Down
16 changes: 16 additions & 0 deletions std/regex/internal/tests.d
Original file line number Diff line number Diff line change
Expand Up @@ -1082,3 +1082,19 @@ alias Sequence(int B, int E) = staticIota!(B, E);
assert(equal!equal(s.matchAll(ctr), outcomes));
assert(equal!equal(s.bmatch(r), outcomes));
}

// bugzilla 17667
@safe unittest
{
import std.algorithm.searching : canFind;
void willThrow(T)(T arg, string msg)
{
auto e = collectException(regex(arg));
assert(e.msg.canFind(msg), e.msg);
}
willThrow([r".", r"[\(\{[\]\}\)]"], "no matching ']' found while parsing character class");
willThrow([r"[\", r"123"], "no matching ']' found while parsing character class");
willThrow([r"[a-", r"123"], "no matching ']' found while parsing character class");
willThrow([r"[a-\", r"123"], "invalid escape sequence");
willThrow([r"\", r"123"], "invalid escape sequence");
}