Commit a776087e authored by Joe Tsai's avatar Joe Tsai Committed by Joe Tsai

runtime: allow calling Func.Name on nil pointer

The Func type has allowed calling the Func.Name method on a nil pointer
since Go1.2, where it returned an empty string. A regression caused by
CL/37331 caused this behavior to change. This breaks code that lazily
does runtime.FuncForPC(myPtr).Name() without first checking that myPtr
is actually non-nil.

Fixes #20872

Change-Id: Iae9a2ebabca5e9d1f5a2cdaf2f30e9c6198fec4f
Reviewed-on: https://go-review.googlesource.com/47354Reviewed-by: default avatarMarvin Stenger <marvin.stenger94@gmail.com>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 445652f4
......@@ -579,6 +579,9 @@ func FuncForPC(pc uintptr) *Func {
// Name returns the name of the function.
func (f *Func) Name() string {
if f == nil {
return ""
}
return funcname(f.funcInfo())
}
......
......@@ -154,3 +154,14 @@ func TestLineNumber(t *testing.T) {
}
}
}
func TestNilName(t *testing.T) {
defer func() {
if ex := recover(); ex != nil {
t.Fatalf("expected no nil panic, got=%v", ex)
}
}()
if got := (*runtime.Func)(nil).Name(); got != "" {
t.Errorf("Name() = %q, want %q", got, "")
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment