mirror of
https://github.com/DeusData/codebase-memory-mcp.git
synced 2026-10-25 06:33:13 +08:00
tree-sitter's GLR parser merges ambiguous parse-stack heads recursively in stack_node_add_link (internal/cbm/vendored/ts_runtime/src/stack.c), once per nesting level. On pathologically nested input whose grammar is ambiguous (Perl's paren-optional calls, f(f(f(...)))), that recursion overflows the native stack during the parse — a small ~1 MB Windows thread stack, and even an 8 MB POSIX stack at extreme depth — before any extraction runs. Grammars that are unambiguous here (Java, C++) never trigger the recursive merge. Cap the recursive merge at CBM_TS_STACK_MERGE_MAX_DEPTH (512) via a depth-tracked inner worker; the public stack_node_add_link is a thin wrapper seeding depth 0, so every existing call site is unchanged. Past the cap the ambiguity is left on the GLR stack instead of merged — a valid parse, never a wrong one. 512 frames is well within a 1 MB stack while far exceeding any realistic source nesting. Add perl_glr_deep_parse_recursion_capped: a crash-isolating regression that parses deep ambiguous Perl directly (a forked child on POSIX, in-process on Windows), bypassing cbm_extract_file's CBM_PERL_MAX_PARSE_NESTING pre-parse guard so the vendored cap itself is exercised rather than the guard. Narrow vendored-runtime change; no other parsing behavior is affected. The higher-level Perl pre-parse guard (issue #913 context) stays in place and can be retired in a follow-up once this cap is proven in the field. Signed-off-by: Shane McCarron <shane.mccarron@corvexconnect.com>