Skip to content

Commit 7dc07d9

Browse files
committed
refactor(php): keep the trait index in the LSP arena
The class-field pass stored the address of a stack-local index in ctx for the duration of the pass. It was restored before returning, but the pattern is what cppcheck's autoVariables check rejects. The index now lives in the call's arena; on an allocation failure it stays NULL and flattening runs the full scan, as before. Signed-off-by: Martin Vogel <[email protected]>
1 parent 9adada0 commit 7dc07d9

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

‎internal/cbm/lsp/php_lsp.c‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4332,9 +4332,16 @@ static void php_lsp_collect_class_fields(PHPLSPContext *ctx, CBMTypeRegistry *re
43324332
* Pass 2: classes and enums (which may `use Trait;` and need the
43334333
* trait's signatures already patched).
43344334
*/
4335-
struct php_trait_index trait_index = {0};
4335+
/* The index lives in the call's arena, not on this stack frame: ctx keeps
4336+
* a pointer to it for the duration of the pass. On an allocation failure
4337+
* it stays NULL and flatten_trait_into_class runs the full scan. */
4338+
struct php_trait_index *trait_index =
4339+
(struct php_trait_index *)cbm_arena_alloc(ctx->arena, sizeof(*trait_index));
4340+
if (trait_index) {
4341+
memset(trait_index, 0, sizeof(*trait_index));
4342+
}
43364343
struct php_trait_index *outer_index = ctx->trait_index;
4337-
ctx->trait_index = &trait_index;
4344+
ctx->trait_index = trait_index;
43384345
for (int pass = 0; pass < 2; pass++) {
43394346
TSNode stack[256];
43404347
int top = 0;
@@ -4359,7 +4366,9 @@ static void php_lsp_collect_class_fields(PHPLSPContext *ctx, CBMTypeRegistry *re
43594366
}
43604367
}
43614368
}
4362-
php_trait_index_free(&trait_index);
4369+
if (trait_index) {
4370+
php_trait_index_free(trait_index);
4371+
}
43634372
ctx->trait_index = outer_index;
43644373
}
43654374

0 commit comments

Comments
 (0)