]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: allow calling Func.Name on nil pointer
authorJoe Tsai <joetsai@digital-static.net>
Fri, 30 Jun 2017 22:44:25 +0000 (15:44 -0700)
committerJoe Tsai <thebrokentoaster@gmail.com>
Fri, 30 Jun 2017 23:42:03 +0000 (23:42 +0000)
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/47354
Reviewed-by: Marvin Stenger <marvin.stenger94@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/runtime/symtab.go
src/runtime/symtab_test.go

index 029c2f15afb5eb3a3db8a6ff4a4a488a19dcbe2c..40add013e425a3b9f0ce9d86908fa360d56f7dbe 100644 (file)
@@ -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())
 }
 
index b75b6b2c2a8f3a65afdcce0ff483f4325dd02379..01e50026590d3070ff9e0a7948dd27bfc363404c 100644 (file)
@@ -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, "")
+       }
+}