Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(dataflow): taking a variable's address in Go is a write of it (#3474)

Yang Bai811bae0b8c61
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/go.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/go.py
index 292f050a1..038f8c20e 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/go.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/go.py
@@ -28,6 +28,7 @@ from .base import (
Occurrence,
Receiver,
StatementDefUse,
+ echoes,
node_text,
)
from .go_values import written_value
@@ -55,6 +56,8 @@ _CONDITIONAL_KINDS = frozenset(
)
# ``case v := <-ch:`` / ``case x = <-ch:`` in a ``select``.
_RECEIVE = "receive_statement"
+# ``&x``: the one unary operator that hands out the variable itself.
+_UNARY = "unary_expression"
# Selector (``obj.field``) and index (``arr[i]``) targets bind no local.
_SELECTOR = "selector_expression"
_INDEX = "index_expression"
@@ -196,6 +199,9 @@ class GoDefUseDialect(BaseDefUseDialect):
defs[start:] = [replace(occ, may=True) for occ in defs[start:]]
self._process(node.child_by_field_name("right"), defs, uses)
return
+ if t == _UNARY and _is_address_of(node):
+ self._address_of(node.child_by_field_name("operand"), defs, uses)
+ return
if t in self.member_access_kinds:
self.collect_reads(node, uses) # receiver is a read; field name is not
return
@@ -208,6 +214,22 @@ class GoDefUseDialect(BaseDefUseDialect):
for child in node.named_children:
self._process(child, defs, uses)
+ def _address_of(
+ self, operand: Node | None, defs: list[Occurrence], uses: list[Occurrence]
+ ) -> None:
+ """``&x`` reads ``x`` and lets whoever holds the address write it:
+ ``fill(&buf)`` can assign ``*dst``, so the variable may change here.
+ A may-def (a def with its paired use), since nothing says the write
+ happens. ``&x.f`` / ``&x[i]`` reach the variable only where the path
+ stays inside the value (:func:`written_value`); through a pointer, a
+ slice or a map the storage is shared and the variable is untouched."""
+ self._process(operand, defs, uses)
+ base = written_value(operand) if operand is not None else None
+ if base is not None:
+ written = [self._occ(base)]
+ defs.extend(written)
+ uses.extend(echoes(written))
+
def _var_spec(self, spec: Node, defs: list[Occurrence], uses: list[Occurrence]) -> None:
"""A ``var_spec`` / ``const_spec``: its direct ``identifier`` children are
the bound names (defs); its ``value`` is a read."""
@@ -242,4 +264,9 @@ class GoDefUseDialect(BaseDefUseDialect):
self._targets(child, defs, uses)
+def _is_address_of(unary: Node) -> bool:
+ op = unary.child_by_field_name("operator")
+ return op is not None and op.text == b"&"
+
+
DIALECT = GoDefUseDialect()
diff --git a/tests/unit/health/test_go_address_of.py b/tests/unit/health/test_go_address_of.py
new file mode 100644
index 000000000..22c3c4202
--- /dev/null
+++ b/tests/unit/health/test_go_address_of.py
@@ -0,0 +1,259 @@
+"""Go: taking a variable's address is a write of it.
+
+``fill(&buf, c)`` hands the callee the variable itself, and the callee may
+assign through it. Read as a plain read of ``buf``, the span around the call
+passed ``buf`` into the helper by value and did not return it: the callee
+then filled the helper's copy and the caller went on with the old one (#3467).
+
+The write may not happen, so it is a may-def: a def with a paired use. The
+address of a field or an element reaches the variable only where the path
+stays inside the value (:func:`go_values.written_value`).
+"""
+
+from __future__ import annotations
+
+import textwrap
+
+import pytest
+
+from repowise.core.analysis.health.complexity.languages import get_language_map
+from repowise.core.analysis.health.dataflow import analyze_file, find_extractions
+
+_TYPES = """
+package m
+
+type P struct {
+ x int
+ q Q
+ ptr *Q
+ xs []int
+ arr [4]int
+}
+type Q struct{ x int }
+
+var shared P
+
+func fill(dst *[]byte, b byte) { *dst = append(*dst, b) }
+func load(dst *P) error { return nil }
+"""
+
+
+def _functions(body: str):
+ try:
+ from repowise.core.ingestion.parser import _get_language
+ except ImportError:
+ pytest.skip("tree-sitter language pack missing for go")
+ if _get_language("go") is None:
+ pytest.skip("tree-sitter language pack missing for go")
+ src = _TYPES + textwrap.dedent(body)
+ return analyze_file("m.go", "go", src.encode(), flagged_only=False).functions
+
+
+def _f(body: str):
+ return next(fn for fn in _functions(body) if fn.name == "f")
+
+
+def _at_marker(signature: str, statement: str, before: str = ""):
+ """The analysis of ``f`` and the line of *statement*, which follows a marker."""
+ fn = _f(f"\nfunc f({signature}) {{\n{before}\n marker := 0\n{statement}\n _ = marker\n}}\n")
+ marker = next(d.line for d in fn.def_use.definitions if d.var == "marker")
+ return fn, marker + 1
+
+
+def _writes(signature: str, statement: str, before: str = "", var: str = "p") -> bool:
+ fn, line = _at_marker(signature, statement, before)
+ return any(d.var == var and d.line == line for d in fn.def_use.definitions)
+
+
+def _reads(signature: str, statement: str, before: str = "", var: str = "p") -> bool:
+ fn, line = _at_marker(signature, statement, before)
+ return any(
+ u.name == var and u.line == line and not u.echo
+ for b in fn.def_use.blocks.values()
+ for u in b.uses
+ )
+
+
+def _may(signature: str, statement: str, before: str = "", var: str = "p") -> bool:
+ """The write is recorded with its paired use, as a write that may not happen."""
+ fn, line = _at_marker(signature, statement, before)
+ return any(
+ u.name == var and u.line == line and u.echo
+ for b in fn.def_use.blocks.values()
+ for u in b.uses
+ )
+
+
+# -- the address of the variable itself -------------------------------------------
+
+
+ ("signature", "before"),
+ [
+ ("p P", ""), # a struct
+ ("p []byte", ""), # a slice: the callee can replace the header
+ ("p map[string]int", ""), # a map: the callee can replace the map
+ ("p *P", ""), # a pointer: the callee can repoint it
+ ("p int", ""),
+ ("", " var p P"),
+ ("", " p := make([]byte, 0, 8)"),
+ ],
+ ids=["struct", "slice", "map", "pointer", "int", "var", "short var"],
+)
+def test_address_of_a_local_is_a_write_and_a_read(signature, before):
+ assert _writes(signature, " use(&p)", before)
+ assert _reads(signature, " use(&p)", before)
+ assert _may(signature, " use(&p)", before)
+
+
+ "statement",
+ [
+ " q := &p\n _ = q",
+ " use(1, &p, 2)",
+ " x := T{dst: &p}\n _ = x",
+ " go use(&p)",
+ " defer use(&p)",
+ " use((&p))",
+ " if err := load(&p); err != nil {\n return\n }",
+ ],
+ ids=["stored", "among arguments", "in a literal", "go", "defer", "parenthesised", "if head"],
+)
+def test_address_of_is_a_write_wherever_it_stands(statement):
+ assert _writes("p P", statement)
+
+
+def test_address_of_a_package_variable_is_no_write_of_a_local():
+ fn, line = _at_marker("", " use(&shared)")
+ assert not any(d.line == line for d in fn.def_use.definitions)
+
+
+def test_a_plain_argument_is_still_only_a_read():
+ assert not _writes("p P", " use(p)")
+ assert _reads("p P", " use(p)")
+
+
+def test_other_unary_operators_do_not_write():
+ assert not _writes("p int", " use(-p)")
+ assert not _writes("p bool", " use(!p)")
+ assert not _writes("p *P", " use(*p)")
+ assert not _writes("p chan int", " use(<-p)")
+
+
+# -- the address of a part of the variable ----------------------------------------
+
+
+ ("signature", "operand", "written"),
+ [
+ ("p P", "p.x", True), # a field of a struct held by value
+ ("p P", "p.q.x", True),
+ ("p P", "p.arr[0]", True), # an element of an array held by value
+ ("p [4]int", "p[1]", True),
+ ("p *P", "p.x", False), # through a pointer: storage the caller shares
+ ("p P", "p.ptr.x", False),
+ ("p P", "p.xs[0]", False), # a slice's elements are shared
+ ("p []int", "p[0]", False),
+ ("p map[string]P", 'p["k"]', False),
+ ],
+)
+def test_address_of_a_part_writes_the_variable_only_inside_the_value(signature, operand, written):
+ assert _writes(signature, f" use(&{operand})") is written
+ assert _reads(signature, f" use(&{operand})")
+
+
+def test_address_of_a_part_of_an_unknown_type_counts_as_a_write():
+ # ``Other`` is declared in another file: returning the variable is right
+ # whichever it holds.
+ assert _writes("p Other", " use(&p.x)")
+
+
+# -- what the slicer makes of it ----------------------------------------------------
+
+_SQUEEZE = """
+func squeeze(s string, c byte) string {
+ if len(s) == 0 {
+ return s
+ }
+ buf := make([]byte, 0, 64)
+ r := 0
+ w := 0
+ for n := len(s); r < n; {
+ if s[r] == c {
+ {FIRST}
+ w++
+ r++
+ for r < n && s[r] == c {
+ r++
+ }
+ } else {
+ {SECOND}
+ w++
+ r++
+ }
+ }
+ if len(buf) == 0 {
+ return s[:w]
+ }
+ return string(buf[:w])
+}
+"""
+
+
+def _spans_holding_the_loop(first: str, second: str) -> list[tuple]:
+ body = _SQUEEZE.replace("{FIRST}", first).replace("{SECOND}", second)
+ fn = next(f for f in _functions(body) if f.name == "squeeze")
+ loop = next(
+ st.start_line
+ for b in fn.cfg.blocks
+ for st in b.statements
+ if st.is_head and st.kind == "for_statement"
+ )
+ return [
+ (x.start_line, x.end_line, x.params, x.returns)
+ for x in find_extractions(fn, get_language_map("go"))
+ if x.start_line <= loop <= x.end_line
+ ]
+
+
+def test_no_span_passes_the_buffer_in_and_leaves_it_out_of_the_returns():
+ spans = _spans_holding_the_loop("fill(&buf, c)", "fill(&buf, s[r])")
+ assert [s for s in spans if "buf" in s[2] and "buf" not in s[3]] == []
+
+
+def test_the_in_place_twin_has_no_such_span_either():
+ """The control: the same writes spelled as assignments."""
+ spans = _spans_holding_the_loop("buf = append(buf, c)", "buf = append(buf, s[r])")
+ assert [s for s in spans if "buf" in s[2] and "buf" not in s[3]] == []
+
+
+_UNMARSHAL = """
+func f(data []byte, limit int) (int, error) {
+ {DECL}
+ if err := load(&cfg); err != nil {
+ return 0, err
+ }
+ total := cfg.x
+ if total > limit {
+ total = limit
+ } else {
+ total += 1
+ }
+ println(total)
+ println(limit)
+ return total, nil
+}
+"""
+
+
+def test_a_variable_filled_through_its_address_is_read_as_written_before_its_use():
+ """``var cfg P; load(&cfg)``: the read after it observes the call's write,
+ not only the zero value the declaration gave it."""
+ fn = _f(_UNMARSHAL.replace("{DECL}", "var cfg P"))
+ lines = sorted(d.line for d in fn.def_use.definitions if d.var == "cfg")
+ declared, filled = lines
+ assert filled == declared + 1
+ reads = [
+ u for b in fn.def_use.blocks.values() for u in b.uses if u.name == "cfg" and not u.echo
+ ]
+ assert {u.line for u in reads} == {filled, filled + 3}