text/template: perform value validity checks
Check reflect.Value.IsValid() before calling other reflect.Value methods
that panic on zero values.
Added tests for cases with untyped nils. They panicked without these fixes.
Removed a TODO.
Fixes #12356
Change-Id: I9b5cbed26db09a0a7c36d99a93f8b9729899d51e
Reviewed-on: https://go-review.googlesource.com/14340
Reviewed-by: Rob Pike <r@golang.org>
diff --git a/src/text/template/exec.go b/src/text/template/exec.go
index 6e46d05..625e9b5 100644
--- a/src/text/template/exec.go
+++ b/src/text/template/exec.go
@@ -135,8 +135,6 @@
*errp = err.Err // Strip the wrapper.
case ExecError:
*errp = err // Keep the wrapper.
- case error: // TODO: This should never happen, but it does. Understand and/or fix.
- *errp = err
default:
panic(e)
}
diff --git a/src/text/template/exec_test.go b/src/text/template/exec_test.go
index 9fd0132..b2ed8e7 100644
--- a/src/text/template/exec_test.go
+++ b/src/text/template/exec_test.go
@@ -337,6 +337,7 @@
{"if not .BinaryFunc call", "{{ if not .BinaryFunc}}{{call .BinaryFunc `1` `2`}}{{else}}No{{end}}", "No", tVal, true},
{"Interface Call", `{{stringer .S}}`, "foozle", map[string]interface{}{"S": bytes.NewBufferString("foozle")}, true},
{".ErrFunc", "{{call .ErrFunc}}", "bla", tVal, true},
+ {"call nil", "{{call nil}}", "", tVal, false},
// Erroneous function calls (check args).
{".BinaryFuncTooFew", "{{call .BinaryFunc `1`}}", "", tVal, false},
@@ -425,12 +426,15 @@
{"slice[1]", "{{index .SI 1}}", "4", tVal, true},
{"slice[HUGE]", "{{index .SI 10}}", "", tVal, false},
{"slice[WRONG]", "{{index .SI `hello`}}", "", tVal, false},
+ {"slice[nil]", "{{index .SI nil}}", "", tVal, false},
{"map[one]", "{{index .MSI `one`}}", "1", tVal, true},
{"map[two]", "{{index .MSI `two`}}", "2", tVal, true},
{"map[NO]", "{{index .MSI `XXX`}}", "0", tVal, true},
- {"map[nil]", "{{index .MSI nil}}", "0", tVal, true},
+ {"map[nil]", "{{index .MSI nil}}", "", tVal, false},
+ {"map[``]", "{{index .MSI ``}}", "0", tVal, true},
{"map[WRONG]", "{{index .MSI 10}}", "", tVal, false},
{"double index", "{{index .SMSI 1 `eleven`}}", "11", tVal, true},
+ {"nil[1]", "{{index nil 1}}", "", tVal, false},
// Len.
{"slice", "{{len .SI}}", "3", tVal, true},
diff --git a/src/text/template/funcs.go b/src/text/template/funcs.go
index ccd0dfc..be13ca2 100644
--- a/src/text/template/funcs.go
+++ b/src/text/template/funcs.go
@@ -104,6 +104,21 @@
return reflect.Value{}, false
}
+// prepareArg checks if value can be used as an argument of type argType, and
+// converts an invalid value to appropriate zero if possible.
+func prepareArg(value reflect.Value, argType reflect.Type) (reflect.Value, error) {
+ if !value.IsValid() {
+ if !canBeNil(argType) {
+ return reflect.Value{}, fmt.Errorf("value is nil; should be of type %s", argType)
+ }
+ value = reflect.Zero(argType)
+ }
+ if !value.Type().AssignableTo(argType) {
+ return reflect.Value{}, fmt.Errorf("value has type %s; should be %s", value.Type(), argType)
+ }
+ return value, nil
+}
+
// Indexing.
// index returns the result of indexing its first argument by the following
@@ -111,6 +126,9 @@
// indexed item must be a map, slice, or array.
func index(item interface{}, indices ...interface{}) (interface{}, error) {
v := reflect.ValueOf(item)
+ if !v.IsValid() {
+ return nil, fmt.Errorf("index of untyped nil")
+ }
for _, i := range indices {
index := reflect.ValueOf(i)
var isNil bool
@@ -125,6 +143,8 @@
x = index.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
x = int64(index.Uint())
+ case reflect.Invalid:
+ return nil, fmt.Errorf("cannot index slice/array with nil")
default:
return nil, fmt.Errorf("cannot index slice/array with type %s", index.Type())
}
@@ -133,17 +153,18 @@
}
v = v.Index(int(x))
case reflect.Map:
- if !index.IsValid() {
- index = reflect.Zero(v.Type().Key())
- }
- if !index.Type().AssignableTo(v.Type().Key()) {
- return nil, fmt.Errorf("%s is not index type for %s", index.Type(), v.Type())
+ index, err := prepareArg(index, v.Type().Key())
+ if err != nil {
+ return nil, err
}
if x := v.MapIndex(index); x.IsValid() {
v = x
} else {
v = reflect.Zero(v.Type().Elem())
}
+ case reflect.Invalid:
+ // the loop holds invariant: v.IsValid()
+ panic("unreachable")
default:
return nil, fmt.Errorf("can't index item of type %s", v.Type())
}
@@ -155,7 +176,11 @@
// length returns the length of the item, with an error if it has no defined length.
func length(item interface{}) (int, error) {
- v, isNil := indirect(reflect.ValueOf(item))
+ v := reflect.ValueOf(item)
+ if !v.IsValid() {
+ return 0, fmt.Errorf("len of untyped nil")
+ }
+ v, isNil := indirect(v)
if isNil {
return 0, fmt.Errorf("len of nil pointer")
}
@@ -172,6 +197,9 @@
// The function must return 1 result, or 2 results, the second of which is an error.
func call(fn interface{}, args ...interface{}) (interface{}, error) {
v := reflect.ValueOf(fn)
+ if !v.IsValid() {
+ return nil, fmt.Errorf("call of nil")
+ }
typ := v.Type()
if typ.Kind() != reflect.Func {
return nil, fmt.Errorf("non-function of type %s", typ)
@@ -201,13 +229,11 @@
} else {
argType = dddType
}
- if !value.IsValid() && canBeNil(argType) {
- value = reflect.Zero(argType)
+
+ var err error
+ if argv[i], err = prepareArg(value, argType); err != nil {
+ return nil, fmt.Errorf("arg %d: %s", i, err)
}
- if !value.Type().AssignableTo(argType) {
- return nil, fmt.Errorf("arg %d has type %s; should be %s", i, value.Type(), argType)
- }
- argv[i] = value
}
result := v.Call(argv)
if len(result) == 2 && !result[1].IsNil() {