Skip to content

Commit

Permalink
Don't compile Regex instances containing negative lookahead (#1761)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Jan 25, 2024
1 parent da39b1d commit 1ddfad3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Jint.Tests/Runtime/EngineTests.ScriptPreparation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,15 @@ public void ScriptPreparationOptimizesConstantReturn()
var result = builtStatement.Execute(new EvaluationContext(_engine)).Value;
Assert.Equal(JsBoolean.False, result);
}

[Fact]
public void CompiledRegexShouldProduceSameResultAsNonCompiled()
{
const string Script = """JSON.stringify(/(.*?)a(?!(a+)b\2c)\2(.*)/.exec("baaabaac"))""";

var nonCompiled = _engine.Evaluate(Script);
var compiled = _engine.Evaluate(Engine.PrepareScript(Script));

Assert.Equal(nonCompiled, compiled);
}
}
5 changes: 4 additions & 1 deletion Jint/Engine.Ast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ public void NodeVisitor(Node node)
{
var regExpLiteral = (RegExpLiteral) literal;
var regExpParseResult = regExpLiteral.ParseResult;
if (regExpParseResult.Success)

// only compile if there's no negative lookahead, it works incorrectly under NET 7 and NET 8
// https://github.com/dotnet/runtime/issues/97455
if (regExpParseResult.Success && !regExpLiteral.Raw.Contains("(?!"))
{
if (!_regexes.TryGetValue(regExpLiteral.Raw, out var regex))
{
Expand Down

0 comments on commit 1ddfad3

Please sign in to comment.