From c0034e672bb9d899a0344eb54bf91edb5fc47582 Mon Sep 17 00:00:00 2001 From: Bill Fumerola Date: Mon, 22 Feb 2016 14:47:17 -0800 Subject: [PATCH] Error out on negative indices in element() --- config/interpolate_funcs.go | 2 +- config/interpolate_funcs_test.go | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index aa609715d..fce60711f 100644 --- a/config/interpolate_funcs.go +++ b/config/interpolate_funcs.go @@ -475,7 +475,7 @@ func interpolationFuncElement() ast.Function { list := StringList(args[0].(string)) index, err := strconv.Atoi(args[1].(string)) - if err != nil { + if err != nil || index < 0 { return "", fmt.Errorf( "invalid number for index, got %s", args[1]) } diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go index 86f738ed6..0b0a07b5b 100644 --- a/config/interpolate_funcs_test.go +++ b/config/interpolate_funcs_test.go @@ -778,6 +778,14 @@ func TestInterpolateFuncElement(t *testing.T) { false, }, + // Negative number should fail + { + fmt.Sprintf(`${element("%s", "-1")}`, + NewStringList([]string{"foo"}).String()), + nil, + true, + }, + // Too many args { fmt.Sprintf(`${element("%s", "0", "2")}`,