Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(lsp): guard trailing append in TS type-text union/intersection parser

Martin Vogel6226972f13f8
diff --git a/internal/cbm/lsp/ts_lsp.c b/internal/cbm/lsp/ts_lsp.c
index ecea2e69d..328990a9e 100644
--- a/internal/cbm/lsp/ts_lsp.c
+++ b/internal/cbm/lsp/ts_lsp.c
@@ -315,8 +315,14 @@ static const CBMType* parse_ts_type_text(CBMArena* arena, const char* text, cons
}
}
if (mc > 0 && (is_union || is_inter)) {
- char* part = cbm_arena_strndup(arena, text + start, len - start);
- members[mc++] = parse_ts_type_text(arena, part, module_qn);
+ // Cap mirrors the in-loop guard: members[16] holds at most 15
+ // real entries + the trailing NULL sentinel. Without this guard,
+ // a union/intersection with >=16 members overflows the array
+ // (UBSan: index 16 out of bounds for 'const CBMType *[16]').
+ if (mc < 15) {
+ char* part = cbm_arena_strndup(arena, text + start, len - start);
+ members[mc++] = parse_ts_type_text(arena, part, module_qn);
+ }
members[mc] = NULL;
return is_union ? cbm_type_union(arena, members, mc)
: cbm_type_intersection(arena, members, mc);
diff --git a/tests/test_ts_lsp.c b/tests/test_ts_lsp.c
index ab4ac733e..06e906b7c 100644
--- a/tests/test_ts_lsp.c
+++ b/tests/test_ts_lsp.c
@@ -800,6 +800,22 @@ TEST(tslsp_union_common_method) {
PASS();
}
+TEST(tslsp_union_many_members_no_overflow) {
+ // Regression: a union with >=16 members used to overflow the
+ // 16-slot members[] array in parse_ts_type_text() — the trailing
+ // post-loop append wrote past the end (UBSan: index 16 out of
+ // bounds). The crash was non-deterministic under pthread parallel
+ // extraction, surfaced by indexing the zod TS codebase. Just
+ // extracting must not crash; resolution behavior is unconstrained.
+ CBMFileResult *r = extract_ts(
+ "function go(x: 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' "
+ "| 'i' | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' "
+ "| 's' | 't') { return x; }\n");
+ ASSERT_NOT_NULL(r);
+ cbm_free_result(r);
+ PASS();
+}
+
/* ── Category 9: more class patterns ───────────────────────────────────────── */
TEST(tslsp_class_static_method) {
@@ -4017,6 +4033,7 @@ SUITE(ts_lsp) {
/* Category 16: union deeper */
RUN_TEST(tslsp_union_common_method);
+ RUN_TEST(tslsp_union_many_members_no_overflow);
/* Category 9: more class patterns */
RUN_TEST(tslsp_class_static_method);