fix(resolution): remove catastrophic backtracking in the Vapor route regex - #1564
Open
pucedoteth wants to merge 1 commit into
Open
fix(resolution): remove catastrophic backtracking in the Vapor route regex#1564pucedoteth wants to merge 1 commit into
pucedoteth wants to merge 1 commit into
Conversation
…regex
The repeated group was `(?:[^,()]+,\s*)*`. `[^,()]+` also matches
whitespace, so every space after a comma could be consumed either by
`[^,()]+` on the next iteration or by `\s*` on the current one. On a
`.get(...)`/`.post(...)` call whose argument list never reaches `use:`,
the match fails and the engine explores every split:
args ms
16 1.7
20 23.8
24 379.7
28 6100.1
That is ~4x per additional argument, so a generated Swift file with a
few dozen arguments stalls index/sync/MCP indefinitely.
Keeping the loop to `<run>,` and matching the gap before `use:` once,
outside the loop, leaves exactly one way to split the input: 2000
arguments now match in 0.2ms. Behaviour is unchanged — `segJoin` only
pulls `"quoted"` literals out of the captured group, so moving the
whitespace out of it cannot alter a route path, and the extracted
receiver/method/path/handler are identical across the real Vapor call
shapes (bare `use:`, string segments, non-string segments like
`BlogUser.parameter`, spaced arguments, and the `Environment.get("X")` /
`req.parameters.get("id")` non-matches).
Fixes colbymchenry#1544
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.
Fixes #1544.
The bug
src/resolution/frameworks/swift.tsmatched Vapor routes with:/\b(\w+)\.(get|post|...)\s*\(\s*((?:[^,()]+,\s*)*)use:\s*([A-Za-z_][\w.]*)/g[^,()]+also matches whitespace, so each space after a comma can be consumed either by\s*on the current iteration or by[^,()]+on the next one. When the call never reachesuse:the match fails, and the engine explores every possible split.Measured on
app.get(arg0, arg1, … x)with nouse:label:~4× per additional argument. At 40 arguments it does not finish — I aborted a run after 10 minutes. Any generated Swift file with a long argument list on a
.get/.post/… call stalls index/sync/MCP indefinitely.The fix
Keep the repeated group to
<run>,and match the gap beforeuse:once, outside the loop:,is excluded from the class, so each iteration is forced to span exactly one comma-delimited run — there is only one way to split the input, and the failure is linear. 2000 arguments now match in 0.2 ms; the 40-argument case is 0.5 ms.Behaviour is unchanged
The capture group is only ever consumed by
segJoin, which pulls"quoted"literals out of it via/"([^"]*)"/g. Moving whitespace outside the group therefore cannot change a route path. I diffed old vs new across the real call shapes and the receiver/method/path/handler are identical on every one:The last two matter:
use:is what discriminates a route fromEnvironment.get(…)/req.parameters.get(…), and both still correctly fail to match.Tests
Added
does not backtrack exponentially on a call that never reaches use: (#1544)to__tests__/frameworks.test.ts— 40 arguments, nouse:, asserts it completes under 1s and extracts no routes. It passes in 0.5 ms with the fix; with the old regex restored it does not terminate, so the assertion fails.npm run build && npx vitest runis green: 2902 passed, 178 skipped, up from 2901 by exactly the new test.Note for anyone reproducing locally: the suite needs
npm run buildfirst — 54 tests in 15 files fail withoutdist/because the CLI tests spawndist/bin/codegraph.js.The three sibling regexes in this file (
(?:\w+\s*,\s*)*in the SwiftUI/UIKit patterns) are not affected —\wand\sare disjoint, so there is no ambiguity to backtrack on. I left them alone.