Potential fix for code scanning alert no. 60: Inefficient regular expression #19
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.
Potential fix for https://github.com/Git-Hub-Chris/MicrosoftVsCode/security/code-scanning/60
To fix the problem, we need to rewrite the regular expression on line 70 to avoid ambiguous subpatterns that could be matched in multiple ways during backtracking—specifically, the
.*and.*?patterns. In this context, the regular expression is used to identify and extract file paths in status/comment lines from git, which almost always use non-whitespace file names (with possible spaces but not newlines or tabs). We should make the capturing patterns more specific, such thatfile1andfile2will not match the'->'separator or newline, and also will not greedily over-match white space separators.We can thus replace
.*with something like\S.*?or[^->\n]+(excluding the'->'token or at least newline and the separator), or more conservatively, match everything up to the next separator or EOL. Commonly, file names in this context do not contain the substring' -> '; so, we can use a negated character group or non-greedy matching up to the next separator.Proposed change:
.*?(forfile1) with a capture like[^\n]+?or perhaps more strictly[^-][^>\n]+?(but that gets awkward).*(forfile2) within the repeated group with a similarly constrained pattern: e.g.,[^\n]+?After testing various cases, an appropriate rewrite would be:
Or, if
'->'can truly never appear in the filename, and separators are' -> ', then:But to keep things simpler and retain as much previous behavior as possible,
[^\n]*?is safe and avoids backtracking.Edit line 70 in
gitEditor.tsto use the updated regex.Suggested fixes powered by Copilot Autofix. Review carefully before merging.