Skip to content

automata: fix out-of-bounds read with a mismatched lazy DFA cache - #1422

Open
safa-rk wants to merge 1 commit into
rust-lang:masterfrom
safa-rk:hybrid-cache-stride-check
Open

safa-rk wants to merge 1 commit into
rust-lang:masterfrom
safa-rk:hybrid-cache-stride-check

Conversation

@safa-rk

@safa-rk safa-rk commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Miri, on a safe hybrid::dfa::DFA::try_search_fwd call:

error: Undefined Behavior: `assume` called with `false`
    --> regex-automata/src/hybrid/dfa.rs:1424:10
     0: regex_automata::hybrid::dfa::DFA::next_state_untagged_unchecked
     1: regex_automata::hybrid::search::find_fwd_imp
     2: regex_automata::hybrid::search::find_fwd
     3: regex_automata::hybrid::dfa::DFA::try_search_fwd

the docs say that using a lazy DFA cache with a DFA it wasn't created for may panic or give wrong results, but when the two DFAs have different strides, the unchecked transition lookup in the search loop reads past the end of cache.trans (a release build segfaults). creating the cache with DFA::new("a") and searching with a byte_classes(false) DFA is enough to hit it. this checks trans.len() == states.len() << stride2 in start_state, where every search starts, and in Lazy::add_state, the only place the table grows, so the misuse panics instead. includes a regression test.

The lazy DFA search loops follow transitions without bounds checks,
which is only in bounds when every state in the cache was laid out with
the DFA's stride. Using a cache with a lazy DFA it wasn't created for
is documented to possibly panic or return incorrect results, but when
the strides differ, the safe search APIs instead read past the end of
the cache's transition table.

We now check that the cache's layout matches the DFA's stride when
computing a start state (every search starts there) and when adding a
state (the only place the transition table grows), so this misuse
panics instead.

This branch has not been deployed

No deployments
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.

1 participant