Skip to content

Commit ae52477

Browse files
committed
cmd/deadcode: fix nil panic in Edge.Site.Pos
Thanks to @ct16k for reporting the crash and suggesting the fix. Also, a test. Fixes golang/go#67915 Change-Id: I72472603e52831d5e571cb8a4e053fd15ff49b72 Reviewed-on: https://go-review.googlesource.com/c/tools/+/591615 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 82be0b4 commit ae52477

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

cmd/deadcode/deadcode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func main() {
260260
edges = append(edges, jsonEdge{
261261
Initial: cond(len(edges) == 0, prettyName(edge.Caller.Func, true), ""),
262262
Kind: cond(isStaticCall(edge), "static", "dynamic"),
263-
Position: toJSONPosition(prog.Fset.Position(edge.Site.Pos())),
263+
Position: toJSONPosition(prog.Fset.Position(edge.Pos())),
264264
Callee: prettyName(edge.Callee.Func, true),
265265
})
266266
}

cmd/deadcode/testdata/issue67915.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Test of -whylive with reflective call
2+
# (regression test for golang/go#67915).
3+
4+
# The live function is reached via reflection:
5+
6+
deadcode example.com
7+
want "unreachable func: dead"
8+
!want "unreachable func: live"
9+
10+
# Reflective calls have Edge.Site=nil, which formerly led to a crash
11+
# when -whylive would compute its position. Now it has NoPos.
12+
13+
deadcode -whylive=example.com.live example.com
14+
want " example.com.main"
15+
want " static@L0006 --> reflect.Value.Call"
16+
want "dynamic@L0000 --> example.com.live"
17+
18+
-- go.mod --
19+
module example.com
20+
go 1.18
21+
22+
-- main.go --
23+
package main
24+
25+
import "reflect"
26+
27+
func main() {
28+
reflect.ValueOf(live).Call(nil)
29+
}
30+
31+
func live() {
32+
println("hello")
33+
}
34+
35+
func dead() {
36+
println("goodbye")
37+
}

go/callgraph/vta/helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func callGraphStr(g *callgraph.Graph) []string {
9999
for f, n := range g.Nodes {
100100
c := make(map[string][]string)
101101
for _, edge := range n.Out {
102-
cs := edge.Site.String()
102+
cs := edge.Site.String() // TODO(adonovan): handle Site=nil gracefully
103103
c[cs] = append(c[cs], funcName(edge.Callee.Func))
104104
}
105105

0 commit comments

Comments
 (0)