Skip to content

Commit

Permalink
go/ssa: fix bug in writeSignature on external functions
Browse files Browse the repository at this point in the history
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 <taking@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
timothy-king committed Jun 14, 2023
1 parent 3b62e7e commit ac29460
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
10 changes: 5 additions & 5 deletions go/ssa/func.go
Expand Up @@ -516,15 +516,15 @@ func (f *Function) relMethod(from *types.Package, recv types.Type) string {
}

// writeSignature writes to buf the signature sig in declaration syntax.
func writeSignature(buf *bytes.Buffer, from *types.Package, name string, sig *types.Signature, params []*Parameter) {
func writeSignature(buf *bytes.Buffer, from *types.Package, name string, sig *types.Signature) {
buf.WriteString("func ")
if recv := sig.Recv(); recv != nil {
buf.WriteString("(")
if n := params[0].Name(); n != "" {
buf.WriteString(n)
if name := recv.Name(); name != "" {
buf.WriteString(name)
buf.WriteString(" ")
}
types.WriteType(buf, params[0].Type(), types.RelativeTo(from))
types.WriteType(buf, recv.Type(), types.RelativeTo(from))
buf.WriteString(") ")
}
buf.WriteString(name)
Expand Down Expand Up @@ -599,7 +599,7 @@ func WriteFunction(buf *bytes.Buffer, f *Function) {
fmt.Fprintf(buf, "# % 3d:\t%s %s\n", i, l.Name(), relType(mustDeref(l.Type()), from))
}
}
writeSignature(buf, from, f.Name(), f.Signature, f.Params)
writeSignature(buf, from, f.Name(), f.Signature)
buf.WriteString(":\n")

if f.Blocks == nil {
Expand Down
3 changes: 3 additions & 0 deletions go/ssa/sanity.go
Expand Up @@ -8,6 +8,7 @@ package ssa
// Currently it checks CFG invariants but little at the instruction level.

import (
"bytes"
"fmt"
"go/types"
"io"
Expand Down Expand Up @@ -412,8 +413,10 @@ func (s *sanity) checkFunction(fn *Function) bool {
s.errorf("nil Prog")
}

var buf bytes.Buffer
_ = fn.String() // must not crash
_ = fn.RelString(fn.relPkg()) // must not crash
WriteFunction(&buf, fn) // must not crash

// All functions have a package, except delegates (which are
// shared across packages, or duplicated as weak symbols in a
Expand Down
4 changes: 2 additions & 2 deletions go/ssa/ssa.go
Expand Up @@ -258,8 +258,8 @@ type Node interface {
// or method.
//
// If Blocks is nil, this indicates an external function for which no
// Go source code is available. In this case, FreeVars and Locals
// are nil too. Clients performing whole-program analysis must
// Go source code is available. In this case, FreeVars, Locals, and
// Params are nil too. Clients performing whole-program analysis must
// handle external functions specially.
//
// Blocks contains the function's control-flow graph (CFG).
Expand Down

0 comments on commit ac29460

Please sign in to comment.