Skip to content

Commit 2534f3b

Browse files
committed
test(repro): crash + trace + gitignore reproductions for #627, #514, #510
- #627: query_graph crash — integer overflow in cypher.c cross_join_with_rels (node_count^2 * growth overflows int -> tiny malloc -> OOB write) on an OPTIONAL MATCH with an already-bound terminal node. Reproduced in a FORKED child (crash must not kill the runner); asserts the child is not signalled. - #514: trace_path data_flow mode drops argument expressions — cbm_edge_info_t carries no properties_json, so the CALLS-edge arg text never reaches the MCP JSON. Asserts the arg expression appears in the data_flow response. - #510: non-root .gitignore ignored — try_load_nested_gitignore() bails when the walk frame prefix is empty, so a directory's own .gitignore is never loaded when indexing a non-git-root subtree. Drives cbm_discover() directly and asserts an explicitly-ignored file is excluded. Signed-off-by: Martin Vogel <[email protected]>
1 parent a8e274c commit 2534f3b

5 files changed

Lines changed: 577 additions & 1 deletion

File tree

‎Makefile.cbm‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,10 @@ TEST_REPRO_SRCS = \
403403
tests/repro/repro_issue480.c \
404404
tests/repro/repro_issue571.c \
405405
tests/repro/repro_issue523.c \
406-
tests/repro/repro_issue546.c
406+
tests/repro/repro_issue546.c \
407+
tests/repro/repro_issue627.c \
408+
tests/repro/repro_issue514.c \
409+
tests/repro/repro_issue510.c
407410

408411
ALL_TEST_SRCS =$(TEST_FOUNDATION_SRCS) $(TEST_EXTRACTION_SRCS) $(TEST_STORE_SRCS) $(TEST_CYPHER_SRCS) $(TEST_MCP_SRCS) $(TEST_DISCOVER_SRCS) $(TEST_GRAPH_BUFFER_SRCS) $(TEST_PIPELINE_SRCS) $(TEST_WATCHER_SRCS) $(TEST_LZ4_SRCS) $(TEST_ZSTD_SRCS) $(TEST_ARTIFACT_SRCS) $(TEST_SQLITE_WRITER_SRCS) $(TEST_GO_LSP_SRCS) $(TEST_C_LSP_SRCS) $(TEST_PHP_LSP_SRCS) $(TEST_CS_LSP_SRCS) $(TEST_CS_LSP_BENCH_SRCS) $(TEST_SCOPE_SRCS) $(TEST_TYPE_REP_SRCS) $(TEST_PY_LSP_SRCS) $(TEST_PY_LSP_BENCH_SRCS) $(TEST_PY_LSP_STRESS_SRCS) $(TEST_PY_LSP_SCALE_SRCS) $(TEST_TS_LSP_SRCS) $(TEST_JAVA_LSP_SRCS) $(TEST_KOTLIN_LSP_SRCS) $(TEST_RUST_LSP_SRCS) $(TEST_TRACES_SRCS) $(TEST_CLI_SRCS) $(TEST_MEM_SRCS) $(TEST_UI_SRCS) $(TEST_HTTPD_SRCS) $(TEST_SECURITY_SRCS) $(TEST_YAML_SRCS) $(TEST_SIMHASH_SRCS) $(TEST_STACK_OVERFLOW_SRCS) $(TEST_INTEGRATION_SRCS)
409412

‎tests/repro/repro_issue510.c‎

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* repro_issue510.c — Reproduce-first case for OPEN bug #510.
3+
*
4+
* Issue: #510 — ".gitignore (non repo root) gaps and overrides"
5+
*
6+
* Root cause (discovered via discover.c):
7+
* cbm_discover_ex() loads the root .gitignore ONLY when a .git directory is
8+
* present at repo_path (is_git_repo gate, ~line 777). For a non-git-root
9+
* call (e.g. indexing pkg/ directly), is_git_repo = false and gitignore =
10+
* NULL. The nested-gitignore fallback also fails: try_load_nested_gitignore()
11+
* has the guard "if (frame->local_gi || frame->prefix[0] == '\0') return NULL"
12+
* (line 630). The initial walk frame always has prefix == "" (empty), so
13+
* prefix[0] == '\0' is true and the function returns NULL without even
14+
* stat-ing the .gitignore file. Result: the .gitignore sitting at the root
15+
* of the indexed directory is completely silently ignored, so every file
16+
* that it excludes gets indexed anyway.
17+
*
18+
* Expected (correct) behaviour:
19+
* When cbm_discover() is called on a directory that is NOT a git repo root
20+
* but DOES contain a .gitignore, that .gitignore MUST be honoured.
21+
* A file matching a pattern in that .gitignore must NOT appear in the
22+
* discovered file list.
23+
*
24+
* Actual (buggy) behaviour:
25+
* cbm_discover() returns the excluded file as a normal discovered file
26+
* because try_load_nested_gitignore() refuses to load .gitignore when
27+
* the walk frame prefix is empty (i.e. the indexed directory itself).
28+
*
29+
* Why RED on current code:
30+
* The fixture creates a directory WITHOUT a .git sub-directory (so the
31+
* is_git_repo gate stays false), writes a .gitignore containing "secret.py",
32+
* and writes secret.py + keep.py. After cbm_discover(), the loop below
33+
* checks that secret.py is NOT in the result. On the current code the
34+
* check FAILS because secret.py is present in the discovered list.
35+
*
36+
* Fix location (not implemented here):
37+
* src/discover/discover.c, function try_load_nested_gitignore():
38+
* Remove (or invert) the "frame->prefix[0] == '\0'" early-return guard so
39+
* that the function also loads .gitignore from the root indexed directory.
40+
* Additionally, cbm_discover_ex() should attempt to load a root .gitignore
41+
* even when the directory is not a git repo.
42+
*/
43+
#include <foundation/compat.h>
44+
#include "test_framework.h"
45+
#include "test_helpers.h"
46+
#include "discover/discover.h"
47+
48+
#include <string.h>
49+
#include <stdlib.h>
50+
#include <stdio.h>
51+
52+
/* ── Fixture ────────────────────────────────────────────────────────────────
53+
*
54+
* Directory layout (NOT a git repo — no .git/ subdir):
55+
*
56+
* <tmpdir>/
57+
* .gitignore <- contains "secret.py"
58+
* secret.py <- should be EXCLUDED by .gitignore
59+
* keep.py <- should be INCLUDED (not matched by any pattern)
60+
*
61+
* Precondition check (to isolate the discovery layer from extraction):
62+
* The root .gitignore is parseable and matches "secret.py".
63+
* cbm_gitignore_matches(gi, "secret.py", false) == true.
64+
* This GREEN precondition proves the matcher itself is correct; if it
65+
* turns RED instead, the bug is in the matcher, not discovery.
66+
*
67+
* Primary assertion (RED on buggy code):
68+
* After cbm_discover(), "secret.py" must NOT appear in the file list.
69+
*
70+
* The test does NOT create a .git directory, mirroring the exact scenario
71+
* from issue #510 Repro 1-A: indexing a sub-package directly rather than
72+
* the repo root.
73+
*/
74+
TEST(repro_issue510_nested_gitignore_honored) {
75+
/* --- set up temp directory --- */
76+
char tmpdir[256];
77+
snprintf(tmpdir, sizeof(tmpdir), "%s/cbm_repro510_XXXXXX", cbm_tmpdir());
78+
ASSERT_NOT_NULL(cbm_mkdtemp(tmpdir));
79+
80+
/* Write fixture files */
81+
ASSERT_EQ(0, th_write_file(TH_PATH(tmpdir, ".gitignore"), "secret.py\n"));
82+
ASSERT_EQ(0, th_write_file(TH_PATH(tmpdir, "secret.py"),
83+
"def secret(): return \"SECRET_TOKEN_111\"\n"));
84+
ASSERT_EQ(0, th_write_file(TH_PATH(tmpdir, "keep.py"),
85+
"def ok(): return 1\n"));
86+
87+
/* --- Precondition: matcher itself handles the pattern correctly --- */
88+
cbm_gitignore_t *gi = cbm_gitignore_parse("secret.py\n");
89+
ASSERT_NOT_NULL(gi);
90+
/* If this assertion fails, the bug is in the gitignore matcher, not
91+
* in discovery — a different bug, not #510. */
92+
ASSERT_TRUE(cbm_gitignore_matches(gi, "secret.py", false));
93+
cbm_gitignore_free(gi);
94+
95+
/* --- Run discovery on the directory (no .git present) --- */
96+
cbm_file_info_t *files = NULL;
97+
int count = 0;
98+
int rc = cbm_discover(tmpdir, NULL, &files, &count);
99+
ASSERT_EQ(0, rc);
100+
101+
/* --- Primary assertion: secret.py must NOT be discovered --- */
102+
bool secret_found = false;
103+
bool keep_found = false;
104+
for (int i = 0; i < count; i++) {
105+
if (strcmp(files[i].rel_path, "secret.py") == 0) {
106+
secret_found = true;
107+
}
108+
if (strcmp(files[i].rel_path, "keep.py") == 0) {
109+
keep_found = true;
110+
}
111+
}
112+
cbm_discover_free(files, count);
113+
th_rmtree(tmpdir);
114+
115+
/* keep.py is a valid Python file and MUST be discovered. */
116+
ASSERT_TRUE(keep_found);
117+
118+
/*
119+
* RED assertion: secret.py matches the root .gitignore pattern and
120+
* must be excluded. On buggy code try_load_nested_gitignore() skips
121+
* the root frame (prefix == ""), so secret.py IS discovered and this
122+
* ASSERT_FALSE fires RED.
123+
*/
124+
ASSERT_FALSE(secret_found);
125+
126+
PASS();
127+
}
128+
129+
/* ── Suite ──────────────────────────────────────────────────────────────── */
130+
131+
SUITE(repro_issue510) {
132+
RUN_TEST(repro_issue510_nested_gitignore_honored);
133+
}

‎tests/repro/repro_issue514.c‎

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
/*
2+
* repro_issue514.c -- Reproduce-first case for OPEN bug #514.
3+
*
4+
* Issue: #514 -- "trace_path data_flow mode doesn't surface arg expressions;
5+
* NestJS DI patterns defeat ~70% of caller resolution"
6+
*
7+
* Sub-claim reproduced: (A) data_flow mode omits argument expressions.
8+
*
9+
* Why sub-claim A over sub-claim B (NestJS DI caller resolution):
10+
* (A) has a crisp binary assertion: the "e" field either appears in the JSON
11+
* output or it does not. (B) is a statistical claim (~70% failure rate) that
12+
* requires a NestJS-specific fixture and a headcount of resolved callers across
13+
* many call sites -- impossible to assert precisely in a unit test. (A) can
14+
* be reproduced with a small two-function Python fixture and one strstr check.
15+
*
16+
* Root cause:
17+
* The MCP schema for trace_path documents data_flow mode as "follow CALLS +
18+
* DATA_FLOWS with arg expressions" (mcp.c line 356-357 and 363-364). Argument
19+
* expressions at each call site ARE stored in the graph: pass_parallel.c::
20+
* append_args_json serializes each CBMCallArg as {"i":<index>,"e":"<expr>,...}
21+
* into the CALLS edge properties_json column. However,
22+
* bfs_to_json_array() (mcp.c ~line 2283) only emits the node fields (name,
23+
* qualified_name, hop, risk, is_test) from cbm_node_hop_t. The edge that
24+
* carried the arg expressions is NOT propagated by cbm_store_bfs() into the
25+
* cbm_traverse_result_t (cbm_edge_info_t carries only from_name, to_name,
26+
* type, confidence -- no properties_json). So even if the user requests
27+
* mode="data_flow", every hop in the response lacks the "args" field and the
28+
* individual arg expression text ("e") is permanently absent from the output.
29+
*
30+
* Expected (correct) behaviour:
31+
* After indexing a two-function Python file where caller() passes a compound
32+
* expression (payload_info + 1) to callee(), a trace_path call with
33+
* mode="data_flow" and direction="outbound" on "caller" must include the
34+
* argument expression text "payload_info" in the response JSON -- either in an
35+
* "args" array inside the hop object, or as a standalone "e" field.
36+
*
37+
* Actual (buggy) behaviour:
38+
* The response is:
39+
* {"function":"caller","direction":"outbound","mode":"data_flow",
40+
* "callees":[{"name":"callee","qualified_name":"...","hop":1}]}
41+
* The hop object contains NO "args" and NO "e"/"arg_expr" field.
42+
* strstr(resp, "payload_info") returns NULL.
43+
*
44+
* Why RED on current code:
45+
* The precondition assertion (CALLS edges >= 1) passes -- edge creation
46+
* and arg serialisation in pass_parallel.c are correct. The final
47+
* ASSERT_NOT_NULL(strstr(resp, "payload_info")) FAILS because
48+
* bfs_to_json_array() never reads or re-emits edge properties_json, so the
49+
* arg expression "payload_info" stored in the CALLS edge is permanently
50+
* discarded before it reaches the MCP JSON output.
51+
*
52+
* Fix location (not implemented here):
53+
* cbm_store_bfs() in src/store/store.c must propagate edge properties_json
54+
* into the cbm_traverse_result_t (extend cbm_edge_info_t or cbm_node_hop_t).
55+
* bfs_to_json_array() in src/mcp/mcp.c must then emit an "args" field when
56+
* mode == "data_flow" and the incoming edge has a non-empty args array.
57+
*/
58+
59+
#include <foundation/compat.h>
60+
#include "test_framework.h"
61+
#include "repro_harness.h"
62+
63+
#include <string.h>
64+
#include <stdlib.h>
65+
#include <stdio.h>
66+
67+
/*
68+
* Fixture: two Python functions in one file.
69+
*
70+
* def callee(x):
71+
* return x * 2
72+
*
73+
* def caller():
74+
* result = callee(payload_info + 1)
75+
* return result
76+
*
77+
* caller() passes the compound expression (payload_info + 1) as the first
78+
* positional argument to callee(). The extractor captures this as a CBMCallArg
79+
* with .expr == "payload_info + 1" (or a prefix thereof after sanitization).
80+
* append_args_json serializes it into the CALLS edge as:
81+
* {"args":[{"i":0,"e":"payload_info + 1"}]}
82+
*
83+
* The expression token "payload_info" is unique enough to identify in the
84+
* output: strstr(resp, "payload_info") is the assertion anchor.
85+
*
86+
* Python is used here because its CALLS extraction (including arg expressions)
87+
* is proven reliable -- see repro_issue480.c for the same fixture approach.
88+
*/
89+
static const RFile k_files[] = {
90+
{
91+
"service.py",
92+
"def callee(x):\n"
93+
" return x * 2\n"
94+
"\n"
95+
"def caller():\n"
96+
" result = callee(payload_info + 1)\n"
97+
" return result\n"
98+
}
99+
};
100+
101+
/*
102+
* TEST: repro_issue514_data_flow_surfaces_arg_expr
103+
*
104+
* Precondition (must be GREEN to prove this is a data_flow surfacing bug):
105+
* rh_count_edges(store, project, "CALLS") >= 1
106+
* If this fires RED, the extractor has a regression unrelated to #514.
107+
*
108+
* Failing assertion (RED on current code):
109+
* strstr(resp, "payload_info") != NULL
110+
* i.e. the argument expression text must appear somewhere in the response.
111+
*/
112+
TEST(repro_issue514_data_flow_surfaces_arg_expr) {
113+
RProj lp;
114+
cbm_store_t *store = rh_index_files(&lp, k_files,
115+
(int)(sizeof(k_files) / sizeof(k_files[0])));
116+
ASSERT_NOT_NULL(store);
117+
118+
/*
119+
* Precondition: at least one CALLS edge must exist after indexing.
120+
* If this fires RED the fixture is broken, not data_flow mode.
121+
* The caller() -> callee(payload_info + 1) call must produce one edge.
122+
*/
123+
int calls_count = rh_count_edges(store, lp.project, "CALLS");
124+
fprintf(stderr,
125+
" [514] CALLS edges=%d (expected>=1; 0=extraction regression)\n",
126+
calls_count);
127+
ASSERT_GT(calls_count, 0);
128+
129+
/*
130+
* Invoke trace_path with mode="data_flow", direction="outbound" on "caller".
131+
*
132+
* Args (matching the trace_path JSON schema in mcp.c ~line 355-374):
133+
* function_name -- "caller": the function that passes the argument
134+
* project -- lp.project: derived from the temp dir
135+
* direction -- "outbound": follow callees (caller -> callee)
136+
* depth -- 2: one hop is enough
137+
* mode -- "data_flow": the mode that promises arg expressions
138+
*
139+
* Expected response (correct):
140+
* {"function":"caller","direction":"outbound","mode":"data_flow",
141+
* "callees":[{"name":"callee","qualified_name":"...","hop":1,
142+
* "args":[{"i":0,"e":"payload_info + 1"}]}]}
143+
* -- or any JSON structure that includes the string "payload_info".
144+
*
145+
* Buggy response:
146+
* {"function":"caller","direction":"outbound","mode":"data_flow",
147+
* "callees":[{"name":"callee","qualified_name":"...","hop":1}]}
148+
* -- no "args", no "e", no "payload_info" anywhere.
149+
*/
150+
char args[512];
151+
snprintf(args, sizeof(args),
152+
"{\"function_name\":\"caller\","
153+
"\"project\":\"%s\","
154+
"\"direction\":\"outbound\","
155+
"\"depth\":2,"
156+
"\"mode\":\"data_flow\"}",
157+
lp.project);
158+
159+
char *resp = cbm_mcp_handle_tool(lp.srv, "trace_path", args);
160+
ASSERT_NOT_NULL(resp);
161+
162+
fprintf(stderr, " [514] trace_path data_flow response: %.400s\n", resp);
163+
164+
/* The response must not be an error -- the node must be found. */
165+
ASSERT_NULL(strstr(resp, "function not found"));
166+
167+
/* The "callees" key must appear (direction=outbound always emits it). */
168+
ASSERT_NOT_NULL(strstr(resp, "\"callees\""));
169+
170+
/* The callees array must be non-empty: "callee" must appear as a hop.
171+
* If this fires RED, the CALLS traversal is broken (separate from #514). */
172+
ASSERT_NULL(strstr(resp, "\"callees\":[]"));
173+
ASSERT_NOT_NULL(strstr(resp, "\"callee\""));
174+
175+
/*
176+
* THE CORE ASSERTION FOR BUG #514:
177+
*
178+
* The argument expression "payload_info" (part of "payload_info + 1" passed
179+
* to callee()) must appear in the response JSON when mode="data_flow".
180+
*
181+
* WHY RED on current code:
182+
* bfs_to_json_array() (mcp.c ~line 2283) only emits cbm_node_hop_t fields
183+
* (name, qualified_name, hop). cbm_edge_info_t (store.h ~line 146) does
184+
* not carry properties_json, so the "e":"payload_info + 1" stored in the
185+
* CALLS edge never reaches the JSON output. strstr returns NULL.
186+
*
187+
* This assertion is the canonical RED line for bug #514.
188+
*/
189+
ASSERT_NOT_NULL(strstr(resp, "payload_info"));
190+
191+
free(resp);
192+
rh_cleanup(&lp, store);
193+
PASS();
194+
}
195+
196+
/* ── Suite ─────────────────────────────────────────────────────────────────── */
197+
SUITE(repro_issue514) {
198+
RUN_TEST(repro_issue514_data_flow_surfaces_arg_expr);
199+
}

0 commit comments

Comments
 (0)