-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Java: Improve Regex flag parsing #15244
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
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
category: fix | ||
--- | ||
* Fixed regular expressions containing flags not being parsed correctly in some cases. |
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 |
---|---|---|
|
@@ -479,7 +479,7 @@ abstract class RegexString extends StringLiteral { | |
private predicate flagGroupStartNoModes(int start, int end) { | ||
this.isGroupStart(start) and | ||
this.getChar(start + 1) = "?" and | ||
this.getChar(start + 2) in ["i", "m", "s", "u", "x", "U"] and | ||
this.getChar(start + 2) in ["-", "i", "d", "m", "s", "u", "x", "U"] and | ||
end = start + 2 | ||
} | ||
|
||
|
@@ -491,15 +491,18 @@ abstract class RegexString extends StringLiteral { | |
this.flagGroupStartNoModes(start, pos) | ||
or | ||
this.modeCharacter(start, pos - 1) and | ||
this.getChar(pos) in ["i", "m", "s", "u", "x", "U"] | ||
this.getChar(pos) in ["-", "i", "d", "m", "s", "u", "x", "U"] | ||
} | ||
|
||
/** | ||
* Holds if a parse mode group is between `start` and `end`. | ||
*/ | ||
private predicate flagGroupStart(int start, int end) { | ||
this.flagGroupStartNoModes(start, _) and | ||
end = max(int i | this.modeCharacter(start, i) | i + 1) | ||
// Check if this is a capturing group with flags, and therefore the `:` should be excluded | ||
exists(int maybeEnd | maybeEnd = max(int i | this.modeCharacter(start, i) | i + 1) | | ||
if this.getChar(maybeEnd) = ":" then end = maybeEnd + 1 else end = maybeEnd | ||
) | ||
} | ||
|
||
/** | ||
|
@@ -510,9 +513,15 @@ abstract class RegexString extends StringLiteral { | |
* ``` | ||
*/ | ||
private predicate flag(string c) { | ||
exists(int pos | | ||
this.modeCharacter(_, pos) and | ||
this.getChar(pos) = c | ||
exists(int start, int pos | | ||
this.modeCharacter(start, pos) and | ||
this.getChar(pos) = c and | ||
// Ignore if flag is disabled; use `<=` to also exclude `-` itself | ||
// This does not properly handle the (contrived) case where a flag is both enabled and | ||
// disabled, e.g. `(?i-i)a+`, in which case the flag seems to acts as if it was disabled | ||
not exists(int minusPos | | ||
this.modeCharacter(start, minusPos) and this.getChar(minusPos) = "-" and minusPos <= pos | ||
) | ||
) | ||
} | ||
|
||
|
@@ -524,6 +533,8 @@ abstract class RegexString extends StringLiteral { | |
exists(string c | this.flag(c) | | ||
c = "i" and result = "IGNORECASE" | ||
or | ||
c = "d" and result = "UNIXLINES" | ||
or | ||
c = "m" and result = "MULTILINE" | ||
or | ||
c = "s" and result = "DOTALL" | ||
|
@@ -930,13 +941,13 @@ class Regex extends RegexString { | |
|
||
/** | ||
* Gets a mode (if any) of this regular expression. Can be any of: | ||
* DEBUG | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed this |
||
* IGNORECASE | ||
* MULTILINE | ||
* DOTALL | ||
* UNICODE | ||
* VERBOSE | ||
* UNICODECLASS | ||
* - IGNORECASE | ||
* - UNIXLINES | ||
* - MULTILINE | ||
* - DOTALL | ||
* - UNICODE | ||
* - VERBOSE | ||
* - UNICODECLASS | ||
*/ | ||
string getAMode() { | ||
result != "None" and | ||
|
@@ -946,7 +957,7 @@ class Regex extends RegexString { | |
} | ||
|
||
/** | ||
* Holds if this regex is used to match against a full string, | ||
* Holds if this regex is used to match against a full string, | ||
* as though it was implicitly surrounded by ^ and $. | ||
*/ | ||
predicate matchesFullString() { matches_full_string = true } | ||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably out of scope, but this predicate might not behave as desired when the flags of a group don't affect the whole pattern, e.g. for
a(?i:b)
it would have the flagi
as result even though this only affects theb
.