2121enum { LEAN_MAX_PARENT_DEPTH = 20 };
2222/* Max positional args to scan for URL/string. */
2323enum { MAX_POSITIONAL_SCAN = 3 };
24- /* Max positional args to scan for handler ref. */
25- enum { MAX_HANDLER_SCAN = 4 };
2624/* Max string arg length before rejection. */
2725enum { MAX_STRING_ARG_LEN = CBM_SZ_512 };
2826/* Min printable ASCII (space). */
@@ -2349,8 +2347,17 @@ static const char *normalize_string_handler(CBMArena *a, const char *raw) {
23492347}
23502348
23512349static const char * extract_handler_arg (CBMExtractCtx * ctx , TSNode args ) {
2350+ /* The LAST eligible argument wins, and every argument is examined.
2351+ * Express, Fastify, gin and Laravel all put middleware between the route
2352+ * path and the handler, so the first function-shaped argument is usually a
2353+ * middleware. Taking the first one pointed the HANDLES edge at the
2354+ * middleware; stopping the scan early missed the handler outright when the
2355+ * middleware was written inline, because an arrow function matches none of
2356+ * the kinds below. Nothing that follows a handler matches them either — an
2357+ * options argument is an object node — so the last match is the handler. */
2358+ const char * handler = NULL ;
23522359 uint32_t nc = ts_node_named_child_count (args );
2353- for (uint32_t ai = HANDLER_START_IDX ; ai < nc && ai < MAX_HANDLER_SCAN ; ai ++ ) {
2360+ for (uint32_t ai = HANDLER_START_IDX ; ai < nc ; ai ++ ) {
23542361 TSNode arg2 = ts_node_named_child (args , ai );
23552362 /* PHP wraps each argument in an `argument` node — unwrap to the value. */
23562363 if (strcmp (ts_node_type (arg2 ), "argument" ) == 0 && ts_node_named_child_count (arg2 ) > 0 ) {
@@ -2362,17 +2369,18 @@ static const char *extract_handler_arg(CBMExtractCtx *ctx, TSNode args) {
23622369 if (strcmp (ak2 , "identifier" ) == 0 || strcmp (ak2 , "member_expression" ) == 0 ||
23632370 strcmp (ak2 , "selector_expression" ) == 0 || strcmp (ak2 , "attribute" ) == 0 ||
23642371 strcmp (ak2 , "field_expression" ) == 0 || strcmp (ak2 , "name" ) == 0 ) {
2365- return cbm_node_text (ctx -> arena , arg2 , ctx -> source );
2372+ handler = cbm_node_text (ctx -> arena , arg2 , ctx -> source );
2373+ continue ;
23662374 }
23672375 if (is_string_like (ak2 )) {
23682376 const char * h =
23692377 normalize_string_handler (ctx -> arena , cbm_node_text (ctx -> arena , arg2 , ctx -> source ));
23702378 if (h && h [0 ]) {
2371- return h ;
2379+ handler = h ;
23722380 }
23732381 }
23742382 }
2375- return NULL ;
2383+ return handler ;
23762384}
23772385
23782386// Extract JSX component refs (uppercase tags) as CALLS edges.
@@ -2965,6 +2973,41 @@ static bool python_receiver_is_exempt(CBMExtractCtx *ctx, TSNode receiver) {
29652973 return false;
29662974}
29672975
2976+ /* Name bound by one Python parameter node, or NULL when the shape binds none.
2977+ * Covers every binding form a `parameters` / `lambda_parameters` list produces:
2978+ * a bare `identifier`, the `name` field of default/typed parameters, and the
2979+ * identifier under a `*args` / `**kwargs` splat. A shape with no identifier (the
2980+ * bare `*` keyword separator) yields NULL and simply matches nothing. */
2981+ /* True when the callee of a BARE Python call `foo()` is bound as a parameter of
2982+ * an enclosing function or lambda -- the bare-call counterpart of
2983+ * python_receiver_is_exempt above.
2984+ *
2985+ * A parameter binding shadows any module-level `foo` for the whole body, so
2986+ * resolving such a call to a project Function/Method by short name alone
2987+ * fabricates the edge BY CONSTRUCTION: `def _run_with_heavy_slot(run): run()`
2988+ * must not bind an unrelated `SatoriLive.run`. Unlike a receiver type this is
2989+ * decidable from the AST outright, with no flow analysis and no list of
2990+ * "generic-looking" callee names -- Python forbids `global` on a parameter, and
2991+ * a parameter is in scope for the entire body regardless of position.
2992+ *
2993+ * The answer is CARRIED BY THE WALK rather than recomputed here. The unified
2994+ * walk binds a def's or lambda's parameters when it opens that scope and
2995+ * unwinds them when it closes it, so this is an O(1) map lookup. Deciding it
2996+ * by ascending the tree instead -- with ts_node_parent() or with a copied walk
2997+ * cursor -- costs O(depth) per call, and since every level of f(f(f(...))) is
2998+ * itself a bare call, that is quadratic across the file: the 30,000-deep
2999+ * fixture in tests/test_stack_overflow.c turned it into a hang rather than a
3000+ * slowdown. An O(1) lookup needs no hop cap, so unlike a capped walk this can
3001+ * no longer fail open on deep-but-ordinary code.
3002+ *
3003+ * It still answers false if the walk's own tracking hit an allocation failure,
3004+ * which costs a suppression and never a true edge. */
3005+ static bool python_callee_is_bound_parameter (CBMExtractCtx * ctx , WalkState * state ,
3006+ TSNode callee_ident ) {
3007+ const char * callee_name = cbm_node_text (ctx -> arena , callee_ident , ctx -> source );
3008+ return cbm_walk_python_param_is_bound (state , callee_name );
3009+ }
3010+
29683011static bool is_objectscript_language (CBMLanguage language ) {
29693012 return language == CBM_LANG_OBJECTSCRIPT_UDL || language == CBM_LANG_OBJECTSCRIPT_ROUTINE ;
29703013}
@@ -3589,11 +3632,18 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
35893632 // (`accelerator.print()` must not bind MockAccelerator.print).
35903633 // Imported receivers stay unflagged: module.function() is Python's
35913634 // canonical cross-file call and the import map resolves it.
3635+ // A BARE Python call foo() whose callee is bound as a parameter of an
3636+ // enclosing scope cannot be the module-level foo, so short-name
3637+ // resolution would fabricate the edge (`def f(run): run()` must not
3638+ // bind SatoriLive.run). Distinct from is_method: there is no receiver
3639+ // here, so the weak-member guard cannot see this class at all.
35923640 if (ctx -> language == CBM_LANG_PYTHON && strcmp (ts_node_type (node ), "call" ) == 0 ) {
35933641 TSNode fn = ts_node_child_by_field_name (node , TS_FIELD ("function" ));
35943642 if (!ts_node_is_null (fn ) && strcmp (ts_node_type (fn ), "attribute" ) == 0 ) {
35953643 TSNode obj = ts_node_child_by_field_name (fn , TS_FIELD ("object" ));
35963644 call .is_method = !python_receiver_is_exempt (ctx , obj );
3645+ } else if (!ts_node_is_null (fn ) && strcmp (ts_node_type (fn ), "identifier" ) == 0 ) {
3646+ call .callee_is_locally_bound = python_callee_is_bound_parameter (ctx , state , fn );
35973647 }
35983648 }
35993649 // TS/JS/TSX receiver-aware guard (#592/#606 direction; same intent
0 commit comments