Repository navigation
Add timeout check during Matches expression eval #12832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gh-worker-dd-mergequeue-cf854d
merged 4 commits into
master
from
jpbempel/add-timeout-in-matches-expr
Oct 14, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 100 additions & 2 deletions
102
...gger/debugger-el/src/main/java/com/datadog/debugger/el/expressions/MatchesExpression.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,113 @@ | ||
| package com.datadog.debugger.el.expressions; | ||
|
|
||
| import com.datadog.debugger.el.EvalContext; | ||
| import com.datadog.debugger.el.EvaluationException; | ||
| import com.datadog.debugger.el.Generated; | ||
| import com.datadog.debugger.el.PrettyPrintVisitor; | ||
| import com.datadog.debugger.el.Value; | ||
| import com.datadog.debugger.el.Visitor; | ||
| import com.datadog.debugger.el.values.StringValue; | ||
| import datadog.trace.bootstrap.debugger.util.TimeoutChecker; | ||
| import java.util.Objects; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| public class MatchesExpression implements BooleanExpression { | ||
| private final ValueExpression<?> sourceString; | ||
| private final StringValue str; | ||
| private final Pattern pattern; | ||
|
|
||
| public class MatchesExpression extends StringPredicateExpression { | ||
| public MatchesExpression(ValueExpression<?> sourceString, StringValue str) { | ||
| super(sourceString, str, String::matches, "matches"); | ||
| this.sourceString = sourceString; | ||
| this.str = str; | ||
| pattern = str != null && str.getValue() != null ? Pattern.compile(str.getValue()) : null; | ||
| } | ||
|
|
||
| @Override | ||
| public <R> R accept(Visitor<R> visitor) { | ||
| return visitor.visit(this); | ||
| } | ||
|
|
||
| @Override | ||
| public Boolean evaluate(EvalContext evalContext) { | ||
| Value<?> sourceValue = | ||
| sourceString != null ? sourceString.evaluate(evalContext) : Value.nullValue(); | ||
| if (sourceValue.isUndefined()) { | ||
| throw new EvaluationException( | ||
| "Cannot evaluate the expression for undefined value", PrettyPrintVisitor.print(this)); | ||
| } | ||
| if (sourceValue.isNull()) { | ||
| throw new EvaluationException( | ||
| "Cannot evaluate the expression for null value", PrettyPrintVisitor.print(this)); | ||
| } | ||
| if (pattern != null && sourceValue.getValue() instanceof String) { | ||
| String sourceStr = (String) sourceValue.getValue(); | ||
| TimeoutCharSequence timeoutCharSequence = | ||
| new TimeoutCharSequence(evalContext.getTimeoutChecker(), this, sourceStr); | ||
| boolean result = pattern.matcher(timeoutCharSequence).matches(); | ||
| ExpressionHelper.checkTimeout(evalContext.getTimeoutChecker(), this); | ||
| return result; | ||
| } | ||
| return Boolean.FALSE; | ||
| } | ||
|
|
||
| public ValueExpression<?> getSourceString() { | ||
| return sourceString; | ||
| } | ||
|
|
||
| public StringValue getStr() { | ||
| return str; | ||
| } | ||
|
|
||
| /** Wraps a String to check the timeout on each char access while the regex is matching. */ | ||
| private static class TimeoutCharSequence implements CharSequence { | ||
| private final TimeoutChecker timeoutChecker; | ||
| private final MatchesExpression matchesExpression; | ||
| private final String s; | ||
| private int charAccessCount; | ||
|
|
||
| TimeoutCharSequence( | ||
| TimeoutChecker timeoutChecker, MatchesExpression matchesExpression, String s) { | ||
| this.timeoutChecker = timeoutChecker; | ||
| this.matchesExpression = matchesExpression; | ||
| this.s = s; | ||
| } | ||
|
|
||
| @Override | ||
| public int length() { | ||
| return s.length(); | ||
| } | ||
|
|
||
| @Override | ||
| public char charAt(int index) { | ||
| if ((charAccessCount++ & 0xFFF) == 0) { | ||
| // checks every 4096 char accesses | ||
| ExpressionHelper.checkTimeout(timeoutChecker, matchesExpression); | ||
| } | ||
| return s.charAt(index); | ||
| } | ||
|
|
||
| @Override | ||
| public CharSequence subSequence(int start, int end) { | ||
| return s.subSequence(start, end); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return s; | ||
| } | ||
| } | ||
|
|
||
| @Generated | ||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| MatchesExpression that = (MatchesExpression) o; | ||
| return Objects.equals(sourceString, that.sourceString) && Objects.equals(str, that.str); | ||
| } | ||
|
|
||
| @Generated | ||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(sourceString, str); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.