Repository navigation
fix(pm): raise pattern too complex when the backtrack budget runs out - #93
Merged
Merged
Conversation
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.
skhaz
approved these changes
Nov 15, 2026
skhaz
approved these changes
Nov 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Defect
When a search exceeds the matcher's backtrack budget, the pattern functions report no match even when one exists:
The only match needs every
a*to give back its input, which takes more thanMaxBacktracks(100000) backtracks.string.match,string.gmatchandstring.gsubreturn the same wrong answer.Cause
vm.backtrackreturns false both when the backtrack stack is empty (a real failure) and when the budget is exhausted, andvm.runturns both into "no match at this position".Findthen moves on to the next start position.Fix
backtrackreports exhaustion aspattern too complex, the same error the parser already raises for patterns that nest too deeply, andrunandFindreturn it. The string library already raisesFinderrors as Lua errors, so all four functions raise it. The budget itself is unchanged.Reference Lua has no backtrack budget and raises
pattern too complexonly 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_MaxBacktracksasserted that exhausting the budget returns zero matches and no error, which is the silent wrong result. It now asserts thepattern too complexerror.TestFind_MaxBacktracksWithExistingMatchcovers the case above, where a match exists.TestStringPatternTooComplexRaiseschecks thatstring.find,string.match,string.gmatchandstring.gsubraise the error.All fail on
main.Verification
go vet ./...,golangci-lintv2.8.0 with the CI flags,go test ./...(full fixture corpus), the coverage collector run, and the race "other" shard (go test -raceon 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.