Skip to content

Commit ac29460

Browse files
committed
go/ssa: fix bug in writeSignature on external functions
Fixes a panic in writeSignature when fn.Params is non-empty while the function has a receiver. fn.Params is nil for non-Go source functions (synthetic or from object files). Change-Id: Iae3f7ce53fca05d1b154349c3b091aee015afa0b Reviewed-on: https://go-review.googlesource.com/c/tools/+/497155 Run-TryBot: Tim King <[email protected]> Reviewed-by: Alan Donovan <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 3b62e7e commit ac29460

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

go/ssa/func.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -516,15 +516,15 @@ func (f *Function) relMethod(from *types.Package, recv types.Type) string {
516516
}
517517

518518
// writeSignature writes to buf the signature sig in declaration syntax.
519-
func writeSignature(buf *bytes.Buffer, from *types.Package, name string, sig *types.Signature, params []*Parameter) {
519+
func writeSignature(buf *bytes.Buffer, from *types.Package, name string, sig *types.Signature) {
520520
buf.WriteString("func ")
521521
if recv := sig.Recv(); recv != nil {
522522
buf.WriteString("(")
523-
if n := params[0].Name(); n != "" {
524-
buf.WriteString(n)
523+
if name := recv.Name(); name != "" {
524+
buf.WriteString(name)
525525
buf.WriteString(" ")
526526
}
527-
types.WriteType(buf, params[0].Type(), types.RelativeTo(from))
527+
types.WriteType(buf, recv.Type(), types.RelativeTo(from))
528528
buf.WriteString(") ")
529529
}
530530
buf.WriteString(name)
@@ -599,7 +599,7 @@ func WriteFunction(buf *bytes.Buffer, f *Function) {
599599
fmt.Fprintf(buf, "# % 3d:\t%s %s\n", i, l.Name(), relType(mustDeref(l.Type()), from))
600600
}
601601
}
602-
writeSignature(buf, from, f.Name(), f.Signature, f.Params)
602+
writeSignature(buf, from, f.Name(), f.Signature)
603603
buf.WriteString(":\n")
604604

605605
if f.Blocks == nil {

go/ssa/sanity.go

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package ssa
88
// Currently it checks CFG invariants but little at the instruction level.
99

1010
import (
11+
"bytes"
1112
"fmt"
1213
"go/types"
1314
"io"
@@ -412,8 +413,10 @@ func (s *sanity) checkFunction(fn *Function) bool {
412413
s.errorf("nil Prog")
413414
}
414415

416+
var buf bytes.Buffer
415417
_ = fn.String() // must not crash
416418
_ = fn.RelString(fn.relPkg()) // must not crash
419+
WriteFunction(&buf, fn) // must not crash
417420

418421
// All functions have a package, except delegates (which are
419422
// shared across packages, or duplicated as weak symbols in a

go/ssa/ssa.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ type Node interface {
258258
// or method.
259259
//
260260
// If Blocks is nil, this indicates an external function for which no
261-
// Go source code is available. In this case, FreeVars and Locals
262-
// are nil too. Clients performing whole-program analysis must
261+
// Go source code is available. In this case, FreeVars, Locals, and
262+
// Params are nil too. Clients performing whole-program analysis must
263263
// handle external functions specially.
264264
//
265265
// Blocks contains the function's control-flow graph (CFG).

0 commit comments

Comments
 (0)