Skip to content

🐛 fix(xpath): charge regex group resets to budget - #2131

Merged
gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/nda73dh0
Nov 15, 2026
Merged

gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/nda73dh0

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

A back-reference pattern could run past its step budget in matches, replace, re:test and re:replace. Clearing the capture slots before each start position was work the budget did not count, so a pattern with many capture groups did more work than its limit allowed. 🛡️ After this change the clearing happens once per search and is charged to the budget before matching starts.

Architecture

The search clears the capture slots once, before the first start position. A failed attempt restores every slot it wrote, so each start position still begins from the state it would have had before. The clearing is charged to the same budget as matching, and a search that spends its budget raises the existing ValueError.

Behavior changes

  • Each search of a pattern with back-references spends two steps per capture group and one per loop register before it starts. A pattern with many groups can reach the step limit and raise ValueError where it returned a result before.
  • Searches within the limit return the same results. Patterns without back-references take the same code path as before.

Fixes #1746.

A back-reference pattern cleared every capture slot again at each start
position, and that work was not counted against the regex step budget.
A pattern with many groups therefore cost the slot count times the
number of positions outside the limit. Clear the slots once per search
instead. A failed attempt unwinds its trail and restores every slot it
wrote, so the next position starts from the same state. The slot count
is charged to the budget before the search begins.

Fixes tox-dev#1746
@gaborbernat gaborbernat added the bug Something isn't working label Nov 15, 2026
@codspeed

codspeed Bot commented Nov 15, 2026

Copy link
Copy Markdown

Merging this PR will regress 1 benchmark

2 warnings

⚠️ 4 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 4 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

⚡ 3 improved benchmarks
❌ 1 regressed benchmark
✅ 578 untouched benchmarks
⏩ 89 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_feature[query-closest] 237.7 µs 274.8 µs -13.48%
⚡ test_mutating_feature[path-xpath-one-cold] 143.2 µs 89.1 µs +60.67%
⚡ test_feature[serialize-inner] 5.8 ms 5.5 ms +5.54%
⚡ test_feature[serialize-inner-indent] 6.2 ms 5.9 ms +5.33%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gaborbernat:sec-fix/nda73dh0 (f8dceb3) with main (fe21f92)

Open in CodSpeed

Footnotes

  1. 89 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@gaborbernat
gaborbernat merged commit 85035a8 into tox-dev:main Nov 15, 2026
29 of 30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XPath regex with a back-reference runs past the step budget on many groups

1 participant