Repository navigation
Fix out-of-bounds reads and sentinel handling in Matcher - #39
Merged
Merged
Conversation
- Add bounds checks in character class parsing ('[', '!', '-')
- Check bounds after '\' and match the escaped character literally,
so '\?' no longer acts as a wildcard
DoMatchSegment returns (char*)-1 to abort backtracking when a star
loop fails. MatchSubpattern picked the longest alternative by pointer
comparison, so this value always won over a real match, and patterns
like "{*.cs,*.txt}" failed to match "main.txt".
ReadOnlySpan<char>.Empty and default spans have a null reference, which made the end-of-path pointer equal to the null "no match" result, so non-matching patterns were reported as matches.
- Add GuardedBuffer that places input right before an inaccessible page, so any read beyond the buffer causes an access violation - Add guard-page tests for boundary patterns in Unix and Windows modes - Add tests for pattern slices, escaped '?', brace alternatives with stars, and empty path spans
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Matcherrelied on look-ahead reads past the end of the pattern. This is harmless forstringinputs, which always have a null terminator, but the publicReadOnlySpan<char>overload accepts arbitrary memory. There, such reads can produce wrong results or crash the process with anAccessViolationException.While fixing this, two more bugs related to internal sentinel values were found.
Fixes
Out-of-bounds reads in character classes and escapes. Patterns ending with
[,[!,[a-,[a-bor\read up to two characters past the end of the span:IsMatch("a", "[a-b]".AsSpan(0, 4))returnedtrue: the]beyond the slice closed the class;An escaped
?is now matched literally:IsMatch("x", @"\?")used to returntrue.Brace alternatives losing to the star-loop abort signal.
(char*)-1comparesgreater than any valid pointer, so it always won the longest-alternative selection:
IsMatch("main.txt", "{*.cs,*.txt}")returnedfalse.Empty path spans backed by a null reference. For
ReadOnlySpan<char>.Empty,the end-of-path pointer was equal to the null "no match" result:
IsMatch(ReadOnlySpan<char>.Empty, "*.cs")returnedtrue.