Skip to content

Commit 4d84406

Browse files
jjoosMartin Vogel
authored andcommitted
fix(extraction): convert fixed-size stacks in extract_channels.c to growable TSNodeStack
The 8 channel-extraction DFS walkers (Python consts, Python, Go, Java, C#, Ruby, Elixir, Rust) used fixed-size `TSNode stack[CHAN_STACK_CAP]` arrays whose push loops guarded with `&& top < CHAN_STACK_CAP`. On deeply-nested ASTs that guard silently dropped children, and an unguarded overflow could corrupt adjacent stack memory — surfacing as a segfault in a later pass (lsp_cross / semantic_edges) on large repos. Convert all 8 to the arena-backed growable TSNodeStack (ts_nstack_init/push/pop), matching the pattern already used in extract_calls.c / extract_imports.c (PR #217). No silent drops, no overflow. Distilled from #339 (trailing-newline cleanup only; conversion is the author's).
1 parent 4fd6e1b commit 4d84406

1 file changed

Lines changed: 56 additions & 56 deletions

File tree

‎internal/cbm/extract_channels.c‎

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,12 @@ static void scan_string_consts_js(CBMExtractCtx *ctx, chan_const_table_t *tbl) {
134134

135135
/* Python constant resolution: NAME = "value" (assignment node). */
136136
static void scan_string_consts_python(CBMExtractCtx *ctx, chan_const_table_t *tbl) {
137-
TSNode stack[CHAN_STACK_CAP];
138-
int top = 0;
139-
stack[top++] = ctx->root;
137+
TSNodeStack stack;
138+
ts_nstack_init(&stack, ctx->arena, CHAN_STACK_CAP);
139+
ts_nstack_push(&stack, ctx->arena, ctx->root);
140140

141-
while (top > 0 && tbl->count < CHAN_CONST_CAP) {
142-
TSNode node = stack[--top];
141+
while (stack.count > 0 && tbl->count < CHAN_CONST_CAP) {
142+
TSNode node = ts_nstack_pop(&stack);
143143
const char *kind = ts_node_type(node);
144144

145145
if (strcmp(kind, "assignment") == 0) {
@@ -162,8 +162,8 @@ static void scan_string_consts_python(CBMExtractCtx *ctx, chan_const_table_t *tb
162162
}
163163

164164
uint32_t count = ts_node_child_count(node);
165-
for (int i = (int)count - SKIP_ONE; i >= 0 && top < CHAN_STACK_CAP; i--) {
166-
stack[top++] = ts_node_child(node, (uint32_t)i);
165+
for (int i = (int)count - SKIP_ONE; i >= 0; i--) {
166+
ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
167167
}
168168
}
169169
}
@@ -548,21 +548,21 @@ static void extract_channels_python(CBMExtractCtx *ctx) {
548548
chan_const_table_t consts = {0};
549549
scan_string_consts_python(ctx, &consts);
550550

551-
TSNode stack[CHAN_STACK_CAP];
552-
int top = 0;
553-
stack[top++] = ctx->root;
551+
TSNodeStack stack;
552+
ts_nstack_init(&stack, ctx->arena, CHAN_STACK_CAP);
553+
ts_nstack_push(&stack, ctx->arena, ctx->root);
554554

555-
while (top > 0) {
556-
TSNode node = stack[--top];
555+
while (stack.count > 0) {
556+
TSNode node = ts_nstack_pop(&stack);
557557
const char *kind = ts_node_type(node);
558558
if (strcmp(kind, "call") == 0) {
559559
py_process_call(ctx, node, &consts);
560560
} else if (strcmp(kind, "decorator") == 0) {
561561
py_process_decorator(ctx, node, &consts);
562562
}
563563
uint32_t count = ts_node_child_count(node);
564-
for (int i = (int)count - SKIP_ONE; i >= 0 && top < CHAN_STACK_CAP; i--) {
565-
stack[top++] = ts_node_child(node, (uint32_t)i);
564+
for (int i = (int)count - SKIP_ONE; i >= 0; i--) {
565+
ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
566566
}
567567
}
568568
}
@@ -626,18 +626,18 @@ static void go_process_call(CBMExtractCtx *ctx, TSNode call) {
626626
}
627627

628628
static void extract_channels_go(CBMExtractCtx *ctx) {
629-
TSNode stack[CHAN_STACK_CAP];
630-
int top = 0;
631-
stack[top++] = ctx->root;
629+
TSNodeStack stack;
630+
ts_nstack_init(&stack, ctx->arena, CHAN_STACK_CAP);
631+
ts_nstack_push(&stack, ctx->arena, ctx->root);
632632

633-
while (top > 0) {
634-
TSNode node = stack[--top];
633+
while (stack.count > 0) {
634+
TSNode node = ts_nstack_pop(&stack);
635635
if (strcmp(ts_node_type(node), "call_expression") == 0) {
636636
go_process_call(ctx, node);
637637
}
638638
uint32_t count = ts_node_child_count(node);
639-
for (int i = (int)count - SKIP_ONE; i >= 0 && top < CHAN_STACK_CAP; i--) {
640-
stack[top++] = ts_node_child(node, (uint32_t)i);
639+
for (int i = (int)count - SKIP_ONE; i >= 0; i--) {
640+
ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
641641
}
642642
}
643643
}
@@ -723,21 +723,21 @@ static void java_process_annotation(CBMExtractCtx *ctx, TSNode annotation) {
723723
}
724724

725725
static void extract_channels_java(CBMExtractCtx *ctx) {
726-
TSNode stack[CHAN_STACK_CAP];
727-
int top = 0;
728-
stack[top++] = ctx->root;
726+
TSNodeStack stack;
727+
ts_nstack_init(&stack, ctx->arena, CHAN_STACK_CAP);
728+
ts_nstack_push(&stack, ctx->arena, ctx->root);
729729

730-
while (top > 0) {
731-
TSNode node = stack[--top];
730+
while (stack.count > 0) {
731+
TSNode node = ts_nstack_pop(&stack);
732732
const char *kind = ts_node_type(node);
733733
if (strcmp(kind, "method_invocation") == 0) {
734734
java_process_call(ctx, node);
735735
} else if (strcmp(kind, "marker_annotation") == 0 || strcmp(kind, "annotation") == 0) {
736736
java_process_annotation(ctx, node);
737737
}
738738
uint32_t count = ts_node_child_count(node);
739-
for (int i = (int)count - SKIP_ONE; i >= 0 && top < CHAN_STACK_CAP; i--) {
740-
stack[top++] = ts_node_child(node, (uint32_t)i);
739+
for (int i = (int)count - SKIP_ONE; i >= 0; i--) {
740+
ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
741741
}
742742
}
743743
}
@@ -792,18 +792,18 @@ static void csharp_process_call(CBMExtractCtx *ctx, TSNode call) {
792792
}
793793

794794
static void extract_channels_csharp(CBMExtractCtx *ctx) {
795-
TSNode stack[CHAN_STACK_CAP];
796-
int top = 0;
797-
stack[top++] = ctx->root;
795+
TSNodeStack stack;
796+
ts_nstack_init(&stack, ctx->arena, CHAN_STACK_CAP);
797+
ts_nstack_push(&stack, ctx->arena, ctx->root);
798798

799-
while (top > 0) {
800-
TSNode node = stack[--top];
799+
while (stack.count > 0) {
800+
TSNode node = ts_nstack_pop(&stack);
801801
if (strcmp(ts_node_type(node), "invocation_expression") == 0) {
802802
csharp_process_call(ctx, node);
803803
}
804804
uint32_t count = ts_node_child_count(node);
805-
for (int i = (int)count - SKIP_ONE; i >= 0 && top < CHAN_STACK_CAP; i--) {
806-
stack[top++] = ts_node_child(node, (uint32_t)i);
805+
for (int i = (int)count - SKIP_ONE; i >= 0; i--) {
806+
ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
807807
}
808808
}
809809
}
@@ -855,18 +855,18 @@ static void ruby_process_call(CBMExtractCtx *ctx, TSNode call) {
855855
}
856856

857857
static void extract_channels_ruby(CBMExtractCtx *ctx) {
858-
TSNode stack[CHAN_STACK_CAP];
859-
int top = 0;
860-
stack[top++] = ctx->root;
858+
TSNodeStack stack;
859+
ts_nstack_init(&stack, ctx->arena, CHAN_STACK_CAP);
860+
ts_nstack_push(&stack, ctx->arena, ctx->root);
861861

862-
while (top > 0) {
863-
TSNode node = stack[--top];
862+
while (stack.count > 0) {
863+
TSNode node = ts_nstack_pop(&stack);
864864
if (strcmp(ts_node_type(node), "call") == 0) {
865865
ruby_process_call(ctx, node);
866866
}
867867
uint32_t count = ts_node_child_count(node);
868-
for (int i = (int)count - SKIP_ONE; i >= 0 && top < CHAN_STACK_CAP; i--) {
869-
stack[top++] = ts_node_child(node, (uint32_t)i);
868+
for (int i = (int)count - SKIP_ONE; i >= 0; i--) {
869+
ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
870870
}
871871
}
872872
}
@@ -957,21 +957,21 @@ static void elixir_process_function_def(CBMExtractCtx *ctx, TSNode func_def) {
957957
}
958958

959959
static void extract_channels_elixir(CBMExtractCtx *ctx) {
960-
TSNode stack[CHAN_STACK_CAP];
961-
int top = 0;
962-
stack[top++] = ctx->root;
960+
TSNodeStack stack;
961+
ts_nstack_init(&stack, ctx->arena, CHAN_STACK_CAP);
962+
ts_nstack_push(&stack, ctx->arena, ctx->root);
963963

964-
while (top > 0) {
965-
TSNode node = stack[--top];
964+
while (stack.count > 0) {
965+
TSNode node = ts_nstack_pop(&stack);
966966
const char *kind = ts_node_type(node);
967967
if (strcmp(kind, "call") == 0) {
968968
elixir_process_call(ctx, node);
969969
} else if (strcmp(kind, "def") == 0) {
970970
elixir_process_function_def(ctx, node);
971971
}
972972
uint32_t count = ts_node_child_count(node);
973-
for (int i = (int)count - SKIP_ONE; i >= 0 && top < CHAN_STACK_CAP; i--) {
974-
stack[top++] = ts_node_child(node, (uint32_t)i);
973+
for (int i = (int)count - SKIP_ONE; i >= 0; i--) {
974+
ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
975975
}
976976
}
977977
}
@@ -1033,18 +1033,18 @@ static void rust_process_call(CBMExtractCtx *ctx, TSNode call) {
10331033
}
10341034

10351035
static void extract_channels_rust(CBMExtractCtx *ctx) {
1036-
TSNode stack[CHAN_STACK_CAP];
1037-
int top = 0;
1038-
stack[top++] = ctx->root;
1036+
TSNodeStack stack;
1037+
ts_nstack_init(&stack, ctx->arena, CHAN_STACK_CAP);
1038+
ts_nstack_push(&stack, ctx->arena, ctx->root);
10391039

1040-
while (top > 0) {
1041-
TSNode node = stack[--top];
1040+
while (stack.count > 0) {
1041+
TSNode node = ts_nstack_pop(&stack);
10421042
if (strcmp(ts_node_type(node), "call_expression") == 0) {
10431043
rust_process_call(ctx, node);
10441044
}
10451045
uint32_t count = ts_node_child_count(node);
1046-
for (int i = (int)count - SKIP_ONE; i >= 0 && top < CHAN_STACK_CAP; i--) {
1047-
stack[top++] = ts_node_child(node, (uint32_t)i);
1046+
for (int i = (int)count - SKIP_ONE; i >= 0; i--) {
1047+
ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
10481048
}
10491049
}
10501050
}

0 commit comments

Comments
 (0)