Hardens the match interoplator against negative arguments.

This commit is contained in:
James Phillips 2016-03-07 13:32:32 -08:00
parent 63c826c2c0
commit a9d640c024
2 changed files with 4 additions and 1 deletions

View File

@ -128,7 +128,7 @@ func (ct *CompiledTemplate) Render(name string) (*structs.PreparedQuery, error)
Variadic: false, Variadic: false,
Callback: func(inputs []interface{}) (interface{}, error) { Callback: func(inputs []interface{}) (interface{}, error) {
i, ok := inputs[0].(int) i, ok := inputs[0].(int)
if ok && i < len(matches) { if ok && i >= 0 && i < len(matches) {
return matches[i], nil return matches[i], nil
} else { } else {
return "", nil return "", nil

View File

@ -214,6 +214,7 @@ func TestTemplate_Render(t *testing.T) {
Service: structs.ServiceQuery{ Service: structs.ServiceQuery{
Service: "${name.prefix} xxx ${name.full} xxx ${name.suffix}", Service: "${name.prefix} xxx ${name.full} xxx ${name.suffix}",
Tags: []string{ Tags: []string{
"${match(-1)}",
"${match(0)}", "${match(0)}",
"${match(1)}", "${match(1)}",
"${match(2)}", "${match(2)}",
@ -243,6 +244,7 @@ func TestTemplate_Render(t *testing.T) {
Service: structs.ServiceQuery{ Service: structs.ServiceQuery{
Service: "hello- xxx hello-foo-bar-none xxx foo-bar-none", Service: "hello- xxx hello-foo-bar-none xxx foo-bar-none",
Tags: []string{ Tags: []string{
"",
"hello-foo-bar-none", "hello-foo-bar-none",
"hello", "hello",
"foo", "foo",
@ -277,6 +279,7 @@ func TestTemplate_Render(t *testing.T) {
"", "",
"", "",
"", "",
"",
"42", "42",
}, },
}, },