From e873af93371e5fc3796d2ec3f0ab2c43c026e039 Mon Sep 17 00:00:00 2001 From: Masayuki Morita Date: Sat, 6 Jan 2018 00:40:48 +0900 Subject: [PATCH] Fix panic with substr interpolation function by invalid offset Fixes #17041 --- config/interpolate_funcs.go | 2 +- config/interpolate_funcs_test.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index 58dee5cd9..72be817a0 100644 --- a/config/interpolate_funcs.go +++ b/config/interpolate_funcs.go @@ -1577,7 +1577,7 @@ func interpolationFuncSubstr() ast.Function { return nil, fmt.Errorf("length should be a non-negative integer") } - if offset > len(str) { + if offset > len(str) || offset < 0 { return nil, fmt.Errorf("offset cannot be larger than the length of the string") } diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go index 9c993a89d..f63d81c30 100644 --- a/config/interpolate_funcs_test.go +++ b/config/interpolate_funcs_test.go @@ -2568,6 +2568,11 @@ func TestInterpolateFuncSubstr(t *testing.T) { nil, true, }, + { + `${substr("foo", -4, -1)}`, + nil, + true, + }, // invalid length {