Skip to content

sre_engine: resume the tail match at prefix_skip, not past the whole prefix - #8473

Merged
youknowone merged 1 commit into
RustPython:mainfrom
youknowone:sre-search-prefix-skip
Aug 9, 2026
Merged

youknowone merged 1 commit into
RustPython:mainfrom
youknowone:sre-search-prefix-skip

Conversation

@youknowone

@youknowone youknowone commented Aug 8, 2026

Copy link
Copy Markdown
Member

re.search returns wrong spans — and misses matches outright — for any pattern whose literal prefix extends past the INFO block's prefix_skip boundary.

>>> re.search(r'ab(cd)', 'xabcdcd')
<re.Match object; span=(1, 7), match='abcdcd'>   # CPython: span=(1, 5), match='abcd'
>>> re.search(r'a(bc)', 'xabc')
None                                             # CPython: span=(1, 4)

re.match / re.fullmatch are unaffected — only the search literal-prefix fast path is wrong. A non-capturing group is also fine, because -(?:ab) sets SRE_INFO_LITERAL and returns early.

Cause

search_info_literal's len > 1 arm advanced the tail-match cursor one character past the matched prefix:

let mut next_ctx = ctx;
if skip != 0 {
    next_ctx.advance_char::<S>();   // ctx.cursor is the LAST prefix char, so this is start + len
} else {
    next_ctx.cursor = state.cursor;
}

ctx.cursor sits on the last prefix character, so advance_char lands on start + prefix_len. The tail has to resume at start + prefix_skip; the two agree only when the prefix happens to end where the skip does.

_get_literal_prefix extends the prefix through a capturing group while pinning prefix_skip at the group boundary, so the two routinely differ. ab(cd) compiles to prefix_len=4, prefix_skip=2:

[INFO, 14, 1, 4, 4, prefix_len=4, prefix_skip=2, 97, 98, 99, 100, 0,0,0,0, LITERAL,97, LITERAL,98, MARK,0, ...]

so the tail resumed two characters too far and re-matched cd at 5..7.

The code position was already advanced correctly by ctx.skip_code(2 * skip) — only the string cursor was wrong.

state.cursor already holds exactly the right value: state.reset(req, req.start) followed by S::skip(&mut state.cursor, skip) makes it req.start + skip. That matches sre_lib.h SRE(search), which sets state->ptr = ptr - (prefix_len - prefix_skip - 1), and it is what the len == 1 arm a few lines above already does (next_ctx.skip_char::<S>(skip)). So the conditional can just go away.

Verification

Both the pristine and patched engine were linked against a corpus stamped with CPython's own re.search answers (30 patterns x 20 subjects = 600 cases), so each is judged against CPython rather than against the other:

mismatches vs CPython
before 94 / 600
after 0 / 600

Many of the 94 are want=(2,6) got=(-1,-1) — the wrong resume drops matches, it does not only widen them.

The added tests/tests.rs::search_literal_prefix_longer_than_skip fails on the unpatched engine (assertion left == right, right: 5) and passes with the fix. The crate's other 19 tests are unchanged and still pass, including the existing info_literal and info_single, which cover the same function's other arms.

commented by Claude

Summary by CodeRabbit

  • Bug Fixes

    • Fixed literal searches with longer prefixes so matching resumes at the correct position.
    • Corrected reported match start and cursor positions for affected searches.
  • Tests

    • Added regression coverage for literal-prefix matching and cursor behavior.

…prefix

`search_info_literal`'s len>1 arm reset the tail-match cursor by advancing one
character past the matched prefix. That position equals the INFO block's
prefix_skip boundary only when the prefix ends where the skip does; for a
pattern like `ab(cd)`, `_get_literal_prefix` reports prefix_len=4 with
prefix_skip=2, so the tail resumed two characters too far.

`state.cursor` already holds `req.start + skip`, which is what
`sre_lib.h` SRE(search) computes as `ptr - (prefix_len - prefix_skip - 1)`,
and which the len==1 arm above already uses. Take it unconditionally.

Searching "xabcdcd" for `ab(cd)` returned span (1, 7) instead of (1, 5);
over a 600-case corpus stamped with CPython's own answers, 94 cases disagreed
before this change and none after. The wrong resume also dropped matches
outright, not only widened them.

Assisted-by: Claude
@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro Plus

Run ID: ffceca61-271f-4ebd-831f-9e4714759ed0

📥 Commits

Reviewing files that changed from the base of the PR and between 1819677 and 4eced76.

📒 Files selected for processing (2)
  • crates/sre_engine/src/engine.rs
  • crates/sre_engine/tests/tests.rs

📝 Walkthrough

Walkthrough

The literal search cursor now resumes at the post-prefix skip position. A regression test covers patterns with a literal prefix longer than the skip value.

Changes

Literal search cursor handling

Layer / File(s) Summary
Correct cursor advancement and regression coverage
crates/sre_engine/src/engine.rs, crates/sre_engine/tests/tests.rs
search_info_literal sets the cursor directly after the prefix skip. The regression test verifies the match start and final cursor position.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: shaharnaveh

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main fix: resuming tail matching at prefix_skip instead of after the full prefix.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@youknowone
youknowone marked this pull request as ready for review August 9, 2026 04:34
@youknowone
youknowone merged commit 557bec1 into RustPython:main Aug 9, 2026
27 checks passed
@youknowone
youknowone deleted the sre-search-prefix-skip branch August 9, 2026 04:48
kyokuping pushed a commit to kyokuping/RustPython that referenced this pull request Aug 9, 2026
…prefix (RustPython#8473)

`search_info_literal`'s len>1 arm reset the tail-match cursor by advancing one
character past the matched prefix. That position equals the INFO block's
prefix_skip boundary only when the prefix ends where the skip does; for a
pattern like `ab(cd)`, `_get_literal_prefix` reports prefix_len=4 with
prefix_skip=2, so the tail resumed two characters too far.

`state.cursor` already holds `req.start + skip`, which is what
`sre_lib.h` SRE(search) computes as `ptr - (prefix_len - prefix_skip - 1)`,
and which the len==1 arm above already uses. Take it unconditionally.

Searching "xabcdcd" for `ab(cd)` returned span (1, 7) instead of (1, 5);
over a 600-case corpus stamped with CPython's own answers, 94 cases disagreed
before this change and none after. The wrong resume also dropped matches
outright, not only widened them.

Assisted-by: Claude
youknowone added a commit that referenced this pull request Sep 16, 2026
…prefix (#8473)

`search_info_literal`'s len>1 arm reset the tail-match cursor by advancing one
character past the matched prefix. That position equals the INFO block's
prefix_skip boundary only when the prefix ends where the skip does; for a
pattern like `ab(cd)`, `_get_literal_prefix` reports prefix_len=4 with
prefix_skip=2, so the tail resumed two characters too far.

`state.cursor` already holds `req.start + skip`, which is what
`sre_lib.h` SRE(search) computes as `ptr - (prefix_len - prefix_skip - 1)`,
and which the len==1 arm above already uses. Take it unconditionally.

Searching "xabcdcd" for `ab(cd)` returned span (1, 7) instead of (1, 5);
over a 600-case corpus stamped with CPython's own answers, 94 cases disagreed
before this change and none after. The wrong resume also dropped matches
outright, not only widened them.

Assisted-by: Claude
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