@@ -1663,27 +1663,39 @@ typedef struct {
16631663 TSNode node ;
16641664 const char * prefix ;
16651665} yaml_walk_frame_t ;
1666- #define YAML_WALK_STACK_CAP CBM_SZ_256
1667-
1668- /* Push block_mapping children of a block_node/block_mapping value onto the walk stack. */
1669- static void push_yaml_block_children (TSNode val , const char * path , yaml_walk_frame_t * stack ,
1670- int * top ) {
1666+ /* On-stack first chunk of the key-path walk; wider mappings spill to the heap
1667+ * through cbm_walk_stack_reserve instead of dropping frames (#2388). */
1668+ #define YAML_WALK_STACK_INLINE CBM_SZ_256
1669+
1670+ /* Push block_mapping children of a block_node/block_mapping value onto the walk
1671+ * stack, growing it as needed. False when the stack could not grow (logged by
1672+ * cbm_walk_stack_reserve as extract.walk_stack_alloc_failed). */
1673+ static bool push_yaml_block_children (TSNode val , const char * path , yaml_walk_frame_t * * stack ,
1674+ int * cap , int * top , const yaml_walk_frame_t * inline_buf ) {
16711675 uint32_t vnc = ts_node_named_child_count (val );
1672- for (int vi = (int )vnc - SKIP_ONE ; vi >= 0 && * top < YAML_WALK_STACK_CAP ; vi -- ) {
1676+ if (!cbm_walk_stack_reserve ((void * * )stack , cap , * top + (int )vnc , sizeof (yaml_walk_frame_t ),
1677+ inline_buf , "yaml_key_paths" )) {
1678+ return false;
1679+ }
1680+ for (int vi = (int )vnc - SKIP_ONE ; vi >= 0 ; vi -- ) {
16731681 TSNode vc = ts_node_named_child (val , (uint32_t )vi );
16741682 const char * vctype = ts_node_type (vc );
16751683 if (strcmp (vctype , "block_mapping" ) == 0 || strcmp (vctype , "block_mapping_pair" ) == 0 ) {
1676- stack [(* top )++ ] = (yaml_walk_frame_t ){vc , path };
1684+ ( * stack ) [(* top )++ ] = (yaml_walk_frame_t ){vc , path };
16771685 }
16781686 }
1687+ return true;
16791688}
16801689
16811690static void walk_yaml_mapping (CBMExtractCtx * ctx , TSNode root , const char * root_prefix ) {
1682- yaml_walk_frame_t stack [YAML_WALK_STACK_CAP ];
1691+ yaml_walk_frame_t inline_stack [YAML_WALK_STACK_INLINE ];
1692+ yaml_walk_frame_t * stack = inline_stack ;
1693+ int cap = YAML_WALK_STACK_INLINE ;
16831694 int top = 0 ;
16841695 stack [top ++ ] = (yaml_walk_frame_t ){root , root_prefix };
1696+ bool grown = true;
16851697
1686- while (top > 0 ) {
1698+ while (grown && top > 0 ) {
16871699 yaml_walk_frame_t frame = stack [-- top ];
16881700 TSNode node = frame .node ;
16891701 const char * prefix = frame .prefix ;
@@ -1709,12 +1721,16 @@ static void walk_yaml_mapping(CBMExtractCtx *ctx, TSNode root, const char *root_
17091721 }
17101722 const char * vk = ts_node_type (val );
17111723 if (strcmp (vk , "block_node" ) == 0 || strcmp (vk , "block_mapping" ) == 0 ) {
1712- push_yaml_block_children (val , path , stack , & top );
1724+ grown = push_yaml_block_children (val , path , & stack , & cap , & top , inline_stack );
1725+ if (!grown ) {
1726+ break ;
1727+ }
17131728 continue ;
17141729 }
17151730 emit_yaml_leaf_value (ctx , val , path );
17161731 }
17171732 }
1733+ cbm_walk_stack_release (stack , inline_stack );
17181734}
17191735
17201736/* ── Infrastructure binding extraction ─────────────────────────────
@@ -1873,9 +1889,13 @@ static void scan_mapping_for_bindings(CBMExtractCtx *ctx, TSNode mapping) {
18731889 emit_infra_bindings (ctx , sources , source_keys , n_sources , targets , n_targets );
18741890}
18751891
1876- #define INFRA_SCAN_STACK_CAP CBM_SZ_512
1892+ /* On-stack first chunk of the binding scan; wider subtrees spill to the heap
1893+ * through cbm_walk_stack_reserve instead of dropping nodes (#2388). */
1894+ #define INFRA_SCAN_STACK_INLINE CBM_SZ_512
18771895static void scan_yaml_for_infra_bindings (CBMExtractCtx * ctx , TSNode root ) {
1878- TSNode stack [INFRA_SCAN_STACK_CAP ];
1896+ TSNode inline_stack [INFRA_SCAN_STACK_INLINE ];
1897+ TSNode * stack = inline_stack ;
1898+ int cap = INFRA_SCAN_STACK_INLINE ;
18791899 int top = 0 ;
18801900 stack [top ++ ] = root ;
18811901 while (top > 0 ) {
@@ -1884,10 +1904,15 @@ static void scan_yaml_for_infra_bindings(CBMExtractCtx *ctx, TSNode root) {
18841904 scan_mapping_for_bindings (ctx , node );
18851905 }
18861906 uint32_t nc = ts_node_named_child_count (node );
1887- for (int i = (int )nc - SKIP_ONE ; i >= 0 && top < INFRA_SCAN_STACK_CAP ; i -- ) {
1907+ if (!cbm_walk_stack_reserve ((void * * )& stack , & cap , top + (int )nc , sizeof (TSNode ),
1908+ inline_stack , "yaml_infra_bindings" )) {
1909+ break ;
1910+ }
1911+ for (int i = (int )nc - SKIP_ONE ; i >= 0 ; i -- ) {
18881912 stack [top ++ ] = ts_node_named_child (node , (uint32_t )i );
18891913 }
18901914 }
1915+ cbm_walk_stack_release (stack , inline_stack );
18911916}
18921917
18931918/* ── HCL infrastructure binding extraction ───────────────────────────
0 commit comments