Skip to content

fix(pm): raise pattern too complex when the backtrack budget runs out - #93

Merged
wolfy-j merged 1 commit into
mainfrom
fix/pm-backtrack-error
Nov 15, 2026
Merged

wolfy-j merged 1 commit into
mainfrom
fix/pm-backtrack-error

Conversation

@wolfy-j

@wolfy-j wolfy-j commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Defect

When a search exceeds the matcher's backtrack budget, the pattern functions report no match even when one exists:

local s = string.rep("a", 15)
local p = "^" .. string.rep("a*", 10) .. string.rep("a", 15) .. "$"
print(string.find(s, p))  -- nil, Lua 5.4: 1 15

The only match needs every a* to give back its input, which takes more than MaxBacktracks (100000) backtracks. string.match, string.gmatch and string.gsub return the same wrong answer.

Cause

vm.backtrack returns false both when the backtrack stack is empty (a real failure) and when the budget is exhausted, and vm.run turns both into "no match at this position". Find then moves on to the next start position.

Fix

backtrack reports exhaustion as pattern too complex, the same error the parser already raises for patterns that nest too deeply, and run and Find return it. The string library already raises Find errors as Lua errors, so all four functions raise it. The budget itself is unchanged.

Reference Lua has no backtrack budget and raises pattern too complex only for its recursion limit. A budgeted matcher cannot prove there is no match once the budget runs out, so it raises instead of reporting a result.

Tests

  • TestFind_MaxBacktracks asserted that exhausting the budget returns zero matches and no error, which is the silent wrong result. It now asserts the pattern too complex error.
  • TestFind_MaxBacktracksWithExistingMatch covers the case above, where a match exists.
  • TestStringPatternTooComplexRaises checks that string.find, string.match, string.gmatch and string.gsub raise the error.

All fail on main.

Verification

go vet ./..., golangci-lint v2.8.0 with the CI flags, go test ./... (full fixture corpus), the coverage collector run, and the race "other" shard (go test -race on all non-fixture tests and the non-regression fixture categories) pass. The regression-fixture race shards are left to CI.

The runtime picks this up through a go-lua dependency bump after merge.

The matcher stopped a search after MaxBacktracks backtracks from one start
position and reported that position as not matching. A pattern whose only
match needs more backtracks than the budget therefore returned nil from
string.find, string.match, string.gmatch and string.gsub, although
reference Lua finds the match.

Exhausting the budget now fails the whole search with a "pattern too
complex" error, which the string library raises as a Lua error.
TestFind_MaxBacktracks asserted the silent no-match result; it now asserts
the error.
@wolfy-j
wolfy-j requested a review from skhaz November 15, 2026 18:42
@wolfy-j
wolfy-j requested a review from skhaz November 15, 2026 18:48
@wolfy-j
wolfy-j requested a review from skhaz November 15, 2026 18:57
@wolfy-j
wolfy-j merged commit 311e725 into main Nov 15, 2026
12 checks passed
@wolfy-j
wolfy-j deleted the fix/pm-backtrack-error branch November 15, 2026 20:04
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