Repository navigation
api: check for overflow in Match::offset too - #182
Merged
Merged
Conversation
Commit 0f3f5da documented the overflow panic of Span::offset and made it explicit via checked_add, but Match::offset kept the unchecked additions: with debug assertions it panics with an 'attempt to add with overflow' message rather than the documented one, and in release builds it silently produces a wrapped, nonsensical span instead of panicking. Delegate to Span::offset and document the panic the same way. Found by running Kani's autoharness (model-checking/kani#3832) over aho-corasick 1.1.4, where both offset methods were reported; upstream already fixed Span::offset, so only Match::offset remained. Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes Match::offset to perform the same checked offsetting behavior as Span::offset, avoiding silent wrapping in release builds and ensuring the panic behavior is consistent with the documented overflow precondition.
Changes:
- Delegate
Match::offsettoSpan::offsetto reuse checked overflow handling. - Document
Match::offset’s overflow panic behavior in the same style asSpan::offset.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+968
to
+972
| /// This panics if adding `offset` to either part of this match's `Span` | ||
| /// would result in overflow. | ||
| #[inline] | ||
| pub fn offset(&self, offset: usize) -> Match { | ||
| Match { | ||
| pattern: self.pattern, | ||
| span: Span { | ||
| start: self.start() + offset, | ||
| end: self.end() + offset, | ||
| }, | ||
| } | ||
| Match { pattern: self.pattern, span: self.span.offset(offset) } |
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.
Commit 0f3f5da ("api: document a couple panicking preconditions") documented the overflow panic of
Span::offsetand made it explicit viachecked_add, butMatch::offsetkept the unchecked additions. Consequently, with debug assertions enabled it panics with the generic "attempt to add with overflow" message rather than the documented one, and in release builds it silently produces a wrapped, nonsensical span instead of panicking:This PR delegates
Match::offsettoSpan::offsetand documents the panic in the same style. Test suite passes.Found by running Kani's autoharness (model-checking/kani#3832) over aho-corasick 1.1.4, where both
offsetmethods were reported;Span::offsetis already fixed on main, so onlyMatch::offsetremained.