Skip to content

Commit

Permalink
Avoid infinite loop when template value is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jan 11, 2019
1 parent afd6994 commit 17a590c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/chezmoi/autotemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func autoTemplate(contents []byte, data map[string]interface{}) ([]byte, error)
sort.Sort(sort.Reverse(byValueLength(variables)))
contentsStr := string(contents)
for _, variable := range variables {
if variable.value == "" {
continue
}
index := strings.Index(contentsStr, variable.value)
for index != -1 && index != len(contentsStr) {
if !inWord(contentsStr, index) && !inWord(contentsStr, index+len(variable.value)) {
Expand Down
8 changes: 8 additions & 0 deletions lib/chezmoi/autotemplate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ func TestAutoTemplate(t *testing.T) {
},
wantStr: "{{ .alpha }} aa a aa {{ .alpha }} aa a aa {{ .alpha }}",
},
{
name: "skip_empty",
contentsStr: "a",
data: map[string]interface{}{
"empty": "",
},
wantStr: "a",
},
} {
t.Run(tc.name, func(t *testing.T) {
got, gotErr := autoTemplate([]byte(tc.contentsStr), tc.data)
Expand Down

0 comments on commit 17a590c

Please sign in to comment.