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.

Bound lambda/method-ref expected-param lookup by declared param count

Martin Vogel0442cceb8017
diff --git a/internal/cbm/lsp/java_lsp.c b/internal/cbm/lsp/java_lsp.c
index fd9bf22ff..c2bb4eda0 100644
--- a/internal/cbm/lsp/java_lsp.c
+++ b/internal/cbm/lsp/java_lsp.c
@@ -2267,6 +2267,17 @@ static uint32_t bind_lambda_args(JavaLSPContext *ctx, TSNode call_node,
if (resolved && resolved->signature && resolved->signature->kind == CBM_TYPE_FUNC) {
param_types = resolved->signature->data.func.param_types;
}
+ /* param_types is NULL-terminated with the DECLARED param count — the call
+ * site may pass MORE arguments (overload mismatch, varargs). Indexing by
+ * the raw argument index read past the terminator and dereferenced
+ * whatever followed in the arena (elasticsearch SIGSEGV; same OOB family
+ * as #427). Bound every access by the array's own length. */
+ int param_type_count = 0;
+ if (param_types) {
+ while (param_types[param_type_count]) {
+ param_type_count++;
+ }
+ }
/* Even without registry param_types, the heuristic (recv_qn + method_name
* → arg shape) often pins the lambda type — that's the path that
* handles `xs.forEach(x -> ...)` and `xs.stream().filter(x -> ...)`
@@ -2296,7 +2307,8 @@ static uint32_t bind_lambda_args(JavaLSPContext *ctx, TSNode call_node,
continue;
/* First try registry-driven SAM inference. */
- const CBMType *expected = param_types ? param_types[i] : NULL;
+ const CBMType *expected =
+ (param_types && i < (uint32_t)param_type_count) ? param_types[i] : NULL;
if (!expected && recv_qn && mname && recv_targ_count > 0) {
/* Heuristic path: bind lambda directly using receiver template
* args + recognized method name, skipping the SAM table. */
@@ -2521,7 +2533,16 @@ static void resolve_method_reference(JavaLSPContext *ctx, TSNode mref,
if (outer_resolved && outer_resolved->signature &&
outer_resolved->signature->kind == CBM_TYPE_FUNC &&
outer_resolved->signature->data.func.param_types) {
- const CBMType *expected = outer_resolved->signature->data.func.param_types[arg_index];
+ /* param_types is NULL-terminated with the DECLARED count; arg_index is
+ * the CALL-SITE index, which can exceed it (overload mismatch,
+ * varargs) — indexing past the terminator dereferences arena garbage
+ * (same OOB family as #427 / bind_lambda_args). */
+ const CBMType *const *pts = outer_resolved->signature->data.func.param_types;
+ int ptc = 0;
+ while (pts[ptc]) {
+ ptc++;
+ }
+ const CBMType *expected = (arg_index >= 0 && arg_index < ptc) ? pts[arg_index] : NULL;
if (expected) {
const char *fi_qn = NULL;
if (expected->kind == CBM_TYPE_NAMED)
diff --git a/tests/test_stack_overflow.c b/tests/test_stack_overflow.c
index b4bc7e79d..b8f0680ee 100644
--- a/tests/test_stack_overflow.c
+++ b/tests/test_stack_overflow.c
@@ -487,6 +487,22 @@ TEST(lsp_cpp_deep_expression_no_crash) {
PASS();
}
+TEST(lsp_java_lambda_args_exceed_params_no_crash) {
+ /* A call with MORE arguments than the resolved method's declared params:
+ * bind_lambda_args indexed the NULL-terminated signature param_types array
+ * by the call-site argument index, reading past the terminator — a garbage
+ * CBMType* then got dereferenced (elasticsearch SIGSEGV, java_lsp.c:2364
+ * via :2722; same OOB family as #427). */
+ const char *src = "class A {\n"
+ " void run(Runnable r) {}\n"
+ " void go() {\n"
+ " run(() -> {}, () -> {}, () -> {}, () -> {}, () -> {}, () -> {});\n"
+ " }\n"
+ "}\n";
+ ASSERT_FALSE(so_extract_crashes(src, CBM_LANG_JAVA, "A.java"));
+ PASS();
+}
+
TEST(lsp_ts_cyclic_types_no_crash) {
const char *src = "type A = B | null;\n"
"type B = A | number;\n"
@@ -510,6 +526,7 @@ SUITE(stack_overflow) {
RUN_TEST(ts_allocator_bound_to_mimalloc_issue424);
RUN_TEST(cpp_large_templated_header_no_crash_issue424);
RUN_TEST(lsp_java_deep_nesting_no_crash);
+ RUN_TEST(lsp_java_lambda_args_exceed_params_no_crash);
RUN_TEST(lsp_cpp_deep_expression_no_crash);
RUN_TEST(lsp_ts_cyclic_types_no_crash);