Skip to content

api: check for overflow in Match::offset too - #182

Merged
BurntSushi merged 1 commit into
BurntSushi:masterfrom
tautschnig:match-offset-checked
Aug 3, 2026
Merged

BurntSushi merged 1 commit into
BurntSushi:masterfrom
tautschnig:match-offset-checked

Conversation

@tautschnig

Copy link
Copy Markdown
Contributor

Commit 0f3f5da ("api: document a couple panicking preconditions") documented the overflow panic of Span::offset and made it explicit via checked_add, but Match::offset kept 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:

let m = Match::must(0, usize::MAX..usize::MAX);
let m2 = m.offset(1); // release: Match with span 0..0

This PR delegates Match::offset to Span::offset and 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 offset methods were reported; Span::offset is already fixed on main, so only Match::offset remained.

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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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::offset to Span::offset to reuse checked overflow handling.
  • Document Match::offset’s overflow panic behavior in the same style as Span::offset.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/util/search.rs
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) }

@BurntSushi BurntSushi left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@BurntSushi
BurntSushi merged commit b68c8d5 into BurntSushi:master Aug 3, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants