Skip to content

automata: don't use the backtracker when it can't search any haystack#1376

Open
kimjune01 wants to merge 1 commit into
rust-lang:masterfrom
kimjune01:fix-1344-backtracker-zero-capacity
Open

automata: don't use the backtracker when it can't search any haystack#1376
kimjune01 wants to merge 1 commit into
rust-lang:masterfrom
kimjune01:fix-1344-backtracker-zero-capacity

Conversation

@kimjune01

@kimjune01 kimjune01 commented Jul 22, 2026

Copy link
Copy Markdown

I picked this up with a failing on main, pass on branch. It seems that the haystack length being expressed by 0 can sometimes be overloaded with what it means. I added a case to disambiguate.


Claude: max_haystack_len() returns 0 both when one position fits and when none do, so the len > max_haystack_len() gate lets an empty haystack through to an engine that can't run it, and is_match unwraps the resulting HaystackTooLong.

The new test panics on master and passes here. cargo test -p regex-automata and -p regex are green, cargo fmt --check is clean.

Fixes #1344

The bounded backtracker needs `len(NFA states) * (len(haystack) + 1)`
bits of visited capacity. When the NFA is big enough relative to that
capacity, it cannot search even an empty haystack.

`BoundedBacktracker::max_haystack_len` saturates to `0` in that case,
which is indistinguishable from "an empty haystack is fine." The meta
engine's length check is `len > max_haystack_len()`, so an empty
haystack passed it, the search returned `HaystackTooLong`, and
`BoundedBacktrackerEngine::is_match` unwrapped that error and panicked.

The meta engine now declines to build the backtracker at all when it
lacks the capacity for a zero-length haystack, which restores the
invariant that this engine is only reachable for haystacks it can
actually search.

Note that only the empty haystack panicked. For any longer haystack the
existing length check already rejected the backtracker.

Fixes rust-lang#1344
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.

BoundedBacktracker::is_match panics on empty haystack when max_haystack_len() saturates to 0

1 participant