Skip to content

automata: check for overflow in Span::offset - #1380

Open
tautschnig wants to merge 2 commits into
rust-lang:masterfrom
tautschnig:span-offset-checked
Open

tautschnig wants to merge 2 commits into
rust-lang:masterfrom
tautschnig:span-offset-checked

Conversation

@tautschnig

Copy link
Copy Markdown

Span is documented as unconstrained ("There are no constraints on the values of a span. It is, for example, legal to create a span where start > end.") and has public fields, but Span::offset performs unchecked additions. With debug assertions enabled it panics with the generic "attempt to add with overflow"; in release builds it silently produces a wrapped, nonsensical span:

let s = Span { start: usize::MAX, end: usize::MAX };
let t = s.offset(1); // release: Span 0..0

This PR uses checked_add and documents the panic, mirroring the equivalent fix already applied to the same code in aho-corasick (BurntSushi/aho-corasick@0f3f5da, and BurntSushi/aho-corasick#182 for the remaining Match::offset). regex-automata lib tests and the regex crate's test suite pass.

Found by running Kani's autoharness (model-checking/kani#3832) over regex-automata 0.4.16; reproduced with plain cargo (no Kani involved) before filing.

Span is documented as unconstrained ("There are no constraints on the
values of a span") and has public fields, but Span::offset performed
unchecked additions: with debug assertions it panics with 'attempt to add
with overflow', and in release builds it silently produces a wrapped,
nonsensical span (e.g. 0..0 from a span at usize::MAX offset by 1).

Use checked_add and document the panic, mirroring the equivalent fix in
aho-corasick (BurntSushi/aho-corasick@0f3f5da).

Found by running Kani's autoharness (model-checking/kani#3832) over
regex-automata 0.4.16.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
Copilot AI review requested due to automatic review settings August 3, 2026 11:27

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 hardens regex-automata’s Span::offset against usize overflow by switching from unchecked addition to checked arithmetic and documenting the resulting panic behavior, preventing silent wraparound spans in release builds.

Changes:

  • Add # Panics documentation to Span::offset describing overflow behavior.
  • Replace start + offset / end + offset with checked_add(...).expect(...) to ensure overflow triggers a deterministic panic.

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

Comment thread regex-automata/src/util/search.rs Outdated
Comment on lines +857 to +858
.expect("invalid start+offset"),
end: self.end.checked_add(offset).expect("invalid end+offset"),
Comment on lines 852 to +853
pub fn offset(&self, offset: usize) -> Span {
Span { start: self.start + offset, end: self.end + offset }
Span {
…Span::offset

Review feedback: mention overflow and the method in the expect messages,
and add a unit test pinning the overflow panic.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
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.

2 participants