Skip to content

Commit 5481a97

Browse files
committed
text/template: indirect interfaces before slicing
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í <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Rob Pike <[email protected]>
1 parent ba66797 commit 5481a97

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/text/template/exec_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ var execTests = []execTest{
502502
{"map MUI64S", "{{index .MUI64S 3}}", "ui643", tVal, true},
503503
{"map MI8S", "{{index .MI8S 3}}", "i83", tVal, true},
504504
{"map MUI8S", "{{index .MUI8S 2}}", "u82", tVal, true},
505+
{"index of an interface field", "{{index .Empty3 0}}", "7", tVal, true},
505506

506507
// Slicing.
507508
{"slice[:]", "{{slice .SI}}", "[3 4 5]", tVal, true},
@@ -527,12 +528,14 @@ var execTests = []execTest{
527528
{"string[1:2]", "{{slice .S 1 2}}", "y", tVal, true},
528529
{"out of range", "{{slice .S 1 5}}", "", tVal, false},
529530
{"3-index slice of string", "{{slice .S 1 2 2}}", "", tVal, false},
531+
{"slice of an interface field", "{{slice .Empty3 0 1}}", "[7]", tVal, true},
530532

531533
// Len.
532534
{"slice", "{{len .SI}}", "3", tVal, true},
533535
{"map", "{{len .MSI }}", "3", tVal, true},
534536
{"len of int", "{{len 3}}", "", tVal, false},
535537
{"len of nothing", "{{len .Empty0}}", "", tVal, false},
538+
{"len of an interface field", "{{len .Empty3}}", "2", tVal, true},
536539

537540
// With.
538541
{"with true", "{{with true}}{{.}}{{end}}", "true", tVal, true},

src/text/template/funcs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,13 @@ func slice(item reflect.Value, indexes ...reflect.Value) (reflect.Value, error)
264264
return reflect.Value{}, fmt.Errorf("invalid slice index: %d > %d", idx[0], idx[1])
265265
}
266266
if len(indexes) < 3 {
267-
return item.Slice(idx[0], idx[1]), nil
267+
return v.Slice(idx[0], idx[1]), nil
268268
}
269269
// given item[i:j:k], make sure i <= j <= k.
270270
if idx[1] > idx[2] {
271271
return reflect.Value{}, fmt.Errorf("invalid slice index: %d > %d", idx[1], idx[2])
272272
}
273-
return item.Slice3(idx[0], idx[1], idx[2]), nil
273+
return v.Slice3(idx[0], idx[1], idx[2]), nil
274274
}
275275

276276
// Length

0 commit comments

Comments
 (0)