]> Cypherpunks repositories - gostls13.git/commitdiff
text/template: indirect interfaces before slicing
authorDaniel Martí <mvdan@mvdan.cc>
Wed, 18 Dec 2019 10:05:59 +0000 (10:05 +0000)
committerDaniel Martí <mvdan@mvdan.cc>
Thu, 19 Dec 2019 13:52:09 +0000 (13:52 +0000)
The recently added slice function used indirectInterface, but then
forgot to actually call reflect.Value.Slice on its result. Calling the
Slice method on the original Value without indirectInterface would
result in a panic, if our slice was indeed behind an interface.

Fix that, and add test cases for all three built-in functions that work
with slices.

Fixes #36199.

Change-Id: I9a18f4f604a3b29967eefeb573f8960000936b88
Reviewed-on: https://go-review.googlesource.com/c/go/+/211877
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
src/text/template/exec_test.go
src/text/template/funcs.go

index aa5cd4c5525c1801114d99dd16f729d6d630cf0b..77294eda4bc56a4afab7ade9d56578aef83fb66f 100644 (file)
@@ -502,6 +502,7 @@ var execTests = []execTest{
        {"map MUI64S", "{{index .MUI64S 3}}", "ui643", tVal, true},
        {"map MI8S", "{{index .MI8S 3}}", "i83", tVal, true},
        {"map MUI8S", "{{index .MUI8S 2}}", "u82", tVal, true},
+       {"index of an interface field", "{{index .Empty3 0}}", "7", tVal, true},
 
        // Slicing.
        {"slice[:]", "{{slice .SI}}", "[3 4 5]", tVal, true},
@@ -527,12 +528,14 @@ var execTests = []execTest{
        {"string[1:2]", "{{slice .S 1 2}}", "y", tVal, true},
        {"out of range", "{{slice .S 1 5}}", "", tVal, false},
        {"3-index slice of string", "{{slice .S 1 2 2}}", "", tVal, false},
+       {"slice of an interface field", "{{slice .Empty3 0 1}}", "[7]", tVal, true},
 
        // Len.
        {"slice", "{{len .SI}}", "3", tVal, true},
        {"map", "{{len .MSI }}", "3", tVal, true},
        {"len of int", "{{len 3}}", "", tVal, false},
        {"len of nothing", "{{len .Empty0}}", "", tVal, false},
+       {"len of an interface field", "{{len .Empty3}}", "2", tVal, true},
 
        // With.
        {"with true", "{{with true}}{{.}}{{end}}", "true", tVal, true},
index 0568c798a842cc5b77c05382154c36b0f84c66eb..46125bc2168b9dffc1545d382b2b9350074706f8 100644 (file)
@@ -264,13 +264,13 @@ func slice(item reflect.Value, indexes ...reflect.Value) (reflect.Value, error)
                return reflect.Value{}, fmt.Errorf("invalid slice index: %d > %d", idx[0], idx[1])
        }
        if len(indexes) < 3 {
-               return item.Slice(idx[0], idx[1]), nil
+               return v.Slice(idx[0], idx[1]), nil
        }
        // given item[i:j:k], make sure i <= j <= k.
        if idx[1] > idx[2] {
                return reflect.Value{}, fmt.Errorf("invalid slice index: %d > %d", idx[1], idx[2])
        }
-       return item.Slice3(idx[0], idx[1], idx[2]), nil
+       return v.Slice3(idx[0], idx[1], idx[2]), nil
 }
 
 // Length