-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
gh-127971: fix off-by-one read beyond the end of a string during search #132574
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 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f0d20b2
gh-127971: do not read past the end of a string
duaneg 462dddf
Add test case
duaneg 1d0210a
Add blurb
duaneg 212452d
Add test case for adaptive_find and comments better explaining the re…
duaneg c07c23e
Tweak conditional phrasing to match loop terminating criteria, add co…
duaneg 338f32d
Address more reviewer feedback: add conditional to adaptive find, tweak
duaneg 9884ec1
Always run test, not just under ASAN
duaneg 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
Next
Next commit
gh-127971: do not read past the end of a string
Fix off-by-one read beyond the end of a string.
- Loading branch information
commit f0d20b254e2444632cdaea2552078ef5399ed0ad
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
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.
I'm totally not sure that
else
branch is appropiate here for 'i == w2.Even since we have
i > w
for thism
this logic isn't clear for me.What about simple replacement
i <= w
for loop condition with classicali < w
?It could be enough and cleaner.
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.
I think the else branch will be OK, since all it does is advance the index, and it is intended and expected that this could potentially advance it past the end of the string, in which case the
for
loop will terminate.We can't just replace the the loop condition with
i < w
since thew
here is the last valid index that the pattern could appear at, and needs to be checked. Otherwise we would miss valid matches, and indeed such a change breaks a large number of unit tests.Note the conditionals that were changed are miss conditions, i.e. the algorithm has determined the character at the index cannot be part of the pattern at this location in the string. The conditionals modified are checking whether the following character could potentially be part of a pattern hit, so as to determine whether to skip it entirely by advancing the full length of the pattern or only as much as possible while still considering it as a valid potential hit. In the case where we are at the end of the buffer it doesn't actually matter which branch we take, since either way it will advance past it and terminate. We just need to avoid reading the invalid following character when it doesn't exist.
Uh oh!
There was an error while loading. Please reload this page.
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.
Maybe it's better to rewrite condition as
i+1 <= w
? It seems to be more obvious way of checking for me.It's very similar to "for" condition, but for another argument and before direct using of
i+1
as index.IMO, rewrited condition slightly reduces cognitive load.