Skip to content
Permalink
Browse files

fix stack overflow error (#7114)

* fixed #7111

Signed-off-by: zwwhdls <zwwhdls@hotmail.com>

* update error message

Signed-off-by: zwwhdls <zwwhdls@hotmail.com>

* add test case

Signed-off-by: zwwhdls <zwwhdls@hotmail.com>

* fix lint error

Signed-off-by: zwwhdls <zwwhdls@hotmail.com>
  • Loading branch information
zwwhdls authored and hickeyma committed Dec 2, 2019
1 parent 22e00be commit 750b870aedd35b69b9ca1e1517635fa70367a309
Showing with 23 additions and 0 deletions.
  1. +8 −0 pkg/engine/engine.go
  2. +15 −0 pkg/engine/engine_test.go
@@ -93,11 +93,19 @@ func warnWrap(warn string) string {
// initFunMap creates the Engine's FuncMap and adds context-specific functions.
func (e Engine) initFunMap(t *template.Template, referenceTpls map[string]renderable) {
funcMap := funcMap()
includedNames := make([]string, 0)

// Add the 'include' function here so we can close over t.
funcMap["include"] = func(name string, data interface{}) (string, error) {
var buf strings.Builder
for _, n := range includedNames {
if n == name {
return "", errors.Wrapf(fmt.Errorf("unable to excute template"), "rendering template has a nested reference name: %s", name)
}
}
includedNames = append(includedNames, name)
err := t.ExecuteTemplate(&buf, name, data)
includedNames = includedNames[:len(includedNames)-1]
return buf.String(), err
}

@@ -460,6 +460,15 @@ func TestAlterFuncMap_include(t *testing.T) {
},
}

// Check nested reference in include FuncMap
d := &chart.Chart{
Metadata: &chart.Metadata{Name: "nested"},
Templates: []*chart.File{
{Name: "templates/quote", Data: []byte(`{{include "nested/templates/quote" . | indent 2}} dead.`)},
{Name: "templates/_partial", Data: []byte(`{{.Release.Name}} - he`)},
},
}

v := chartutil.Values{
"Values": "",
"Chart": c.Metadata,
@@ -477,6 +486,12 @@ func TestAlterFuncMap_include(t *testing.T) {
if got := out["conrad/templates/quote"]; got != expect {
t.Errorf("Expected %q, got %q (%v)", expect, got, out)
}

_, err = Render(d, v)
expectErrName := "nested/templates/quote"
if err == nil {
t.Errorf("Expected err of nested reference name: %v", expectErrName)
}
}

func TestAlterFuncMap_require(t *testing.T) {

0 comments on commit 750b870

Please sign in to comment.
You can’t perform that action at this time.