]> Cypherpunks repositories - gostls13.git/commitdiff
text/template: fix method lookup on addressable nil pointer
authorRuss Cox <rsc@golang.org>
Fri, 27 Jan 2017 19:14:05 +0000 (14:14 -0500)
committerRuss Cox <rsc@golang.org>
Thu, 9 Feb 2017 14:58:40 +0000 (14:58 +0000)
Fixes #18816.

Change-Id: I4f8f1cac2680dbde492c56d3a5a038577605e7c1
Reviewed-on: https://go-review.googlesource.com/36542
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/text/template/exec.go
src/text/template/exec_test.go

index 7d92bd9d36e470ad32d939edc2bf8aca6a7137d0..0e517a6ec3bd3fc90cdeabe0e65f4e24607763bc 100644 (file)
@@ -551,7 +551,7 @@ func (s *state) evalField(dot reflect.Value, fieldName string, node parse.Node,
        // Unless it's an interface, need to get to a value of type *T to guarantee
        // we see all methods of T and *T.
        ptr := receiver
-       if ptr.Kind() != reflect.Interface && ptr.CanAddr() {
+       if ptr.Kind() != reflect.Interface && ptr.Kind() != reflect.Ptr && ptr.CanAddr() {
                ptr = ptr.Addr()
        }
        if method := ptr.MethodByName(fieldName); method.IsValid() {
index 5892b27391b511d8b50bf4cabe44949650ee7490..9f7e637c190a4814e668ead626900a28f69c76ea 100644 (file)
@@ -147,6 +147,8 @@ var tVal = &T{
        Tmpl:                 Must(New("x").Parse("test template")), // "x" is the value of .X
 }
 
+var tSliceOfNil = []*T{nil}
+
 // A non-empty interface.
 type I interface {
        Method0() string
@@ -337,6 +339,7 @@ var execTests = []execTest{
                "true", tVal, true},
        {".NilOKFunc not nil", "{{call .NilOKFunc .PI}}", "false", tVal, true},
        {".NilOKFunc nil", "{{call .NilOKFunc nil}}", "true", tVal, true},
+       {"method on nil value from slice", "-{{range .}}{{.Method1 1234}}{{end}}-", "-1234-", tSliceOfNil, true},
 
        // Function call builtin.
        {".BinaryFunc", "{{call .BinaryFunc `1` `2`}}", "[1=2]", tVal, true},