Skip to content

Commit 5cb90f6

Browse files
fix(extract): drop cgo's import "C" pseudo-package
`import "C"` is not an import: "C" is reserved by go/build and no package with that path can exist — it only tells the toolchain to compile the preceding comment as C. cbm emitted it as an ordinary import, so the import resolver fell through to its symbol-name fallback (cbm_pipeline_resolve_import_node Strategy 3, pass_pkgmap.c), matched the literal name "C" against every project node called C, and picked the lexicographically smallest one. Measured on a real Go+C repo: all 27 files carrying `import "C"` pointed their IMPORTS edge at the same unrelated `C` member of a test helper — 27 false edges, and 27 files whose import map named a package that does not exist. Skip the spec in parse_go_import_spec. Node counts are unchanged and the file's real imports are untouched; only the false edge disappears (a 3-file fixture goes 22 edges -> 21, 14 nodes -> 14; the real repo goes 27 -> 0 such edges). Reproduce-first: go_cgo_pseudo_import_dropped is RED without the extractor change (the "C" import is still in imports[]) and GREEN with it, while still asserting the file's real `fmt` import survives. Signed-off-by: Ilya Brykau <ilya.brykau@orca.security>
1 parent 5fbab7b commit 5cb90f6

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

‎internal/cbm/extract_imports.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ static void parse_go_import_spec(CBMExtractCtx *ctx, TSNode spec) {
133133
return;
134134
}
135135

136+
/* cgo: `import "C"` names the cgo PSEUDO-package, not a real one — "C" is
137+
* reserved by go/build and no package with that import path can exist.
138+
* Emitting it as an ordinary import makes the import resolver fall through
139+
* to its symbol-name fallback (cbm_pipeline_resolve_import_node Strategy 3,
140+
* pass_pkgmap.c), which matches the literal name "C" against every project
141+
* node called C and picks the lexicographically smallest: on a real Go repo
142+
* all 27 cgo files pointed their IMPORTS edge at the same unrelated `C`
143+
* member of a test helper. Drop it — the C side is not a graph package, and
144+
* the file's remaining imports are unaffected. */
145+
if (strcmp(path, "C") == 0) {
146+
return;
147+
}
148+
136149
TSNode name_node = ts_node_child_by_field_name(spec, TS_FIELD("name"));
137150
const char *local_name =
138151
!ts_node_is_null(name_node) ? cbm_node_text(a, name_node, ctx->source) : path_last(a, path);

‎tests/test_extraction.c‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3015,6 +3015,25 @@ TEST(go_imports) {
30153015
PASS();
30163016
}
30173017

3018+
/* cgo's `import "C"` is a pseudo-package, not a real import: keeping it lets the
3019+
* import resolver name-match "C" onto an arbitrary project symbol called C. The
3020+
* real imports of the same file must survive. */
3021+
TEST(go_cgo_pseudo_import_dropped) {
3022+
CBMFileResult *r = extract("package m\n\n/*\nstatic int helper(void) { return 1; }\n*/\n"
3023+
"import \"C\"\n\nimport \"fmt\"\n\n"
3024+
"func Run() { fmt.Println(C.helper()) }\n",
3025+
CBM_LANG_GO, "t", "cgo.go");
3026+
ASSERT_NOT_NULL(r);
3027+
ASSERT_FALSE(r->has_error);
3028+
ASSERT(has_import(r, "fmt"));
3029+
for (int i = 0; i < r->imports.count; i++) {
3030+
ASSERT_NOT_NULL(r->imports.items[i].module_path);
3031+
ASSERT_TRUE(strcmp(r->imports.items[i].module_path, "C") != 0);
3032+
}
3033+
cbm_free_result(r);
3034+
PASS();
3035+
}
3036+
30183037
/* #1935: Go struct fields were never extracted — find_class_body() returns the
30193038
* struct_type node, whose only named child is a field_declaration_list, so the
30203039
* member loop matched nothing and every field was silently skipped (0 Field
@@ -6752,6 +6771,7 @@ SUITE(extraction) {
67526771
RUN_TEST(python_imports);
67536772
RUN_TEST(js_imports);
67546773
RUN_TEST(go_imports);
6774+
RUN_TEST(go_cgo_pseudo_import_dropped);
67556775
RUN_TEST(extract_go_struct_fields_have_nodes);
67566776
RUN_TEST(java_imports);
67576777
RUN_TEST(rust_imports);

0 commit comments

Comments
 (0)