Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit a4b06d6

Browse files
authored
Merge pull request hashicorp#7145 from hashicorp/b-concat-lists-of-maps-panic
core: Fix panic on concat() w/ list of nonprimitives
2 parents 2127466 + 9bc980f commit a4b06d6

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

config/interpolate_funcs.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,18 @@ func interpolationFuncConcat() ast.Function {
240240
// Otherwise variables
241241
if argument, ok := arg.([]ast.Variable); ok {
242242
for _, element := range argument {
243-
finalListElements = append(finalListElements, element.Value.(string))
243+
t := element.Type
244+
switch t {
245+
case ast.TypeString:
246+
finalListElements = append(finalListElements, element.Value.(string))
247+
default:
248+
return nil, fmt.Errorf("concat() does not support lists of %s", t.Printable())
249+
}
244250
}
245251
continue
246252
}
247253

248-
return nil, fmt.Errorf("arguments to concat() must be a string or list")
254+
return nil, fmt.Errorf("arguments to concat() must be a string or list of strings")
249255
}
250256

251257
return stringSliceToVariableValue(finalListElements), nil

config/interpolate_funcs_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io/ioutil"
66
"os"
77
"reflect"
8+
"strings"
89
"testing"
910

1011
"github.com/hashicorp/hil"
@@ -226,6 +227,40 @@ func TestInterpolateFuncConcat(t *testing.T) {
226227
})
227228
}
228229

230+
// TODO: This test is split out and calls a private function
231+
// because there's no good way to get a list of maps into the unit
232+
// tests due to GH-7142 - once lists of maps can be expressed properly as
233+
// literals this unit test can be wrapped back into the suite above.
234+
//
235+
// Reproduces crash reported in GH-7030.
236+
func TestInterpolationFuncConcatListOfMaps(t *testing.T) {
237+
listOfMapsOne := ast.Variable{
238+
Type: ast.TypeList,
239+
Value: []ast.Variable{
240+
{
241+
Type: ast.TypeMap,
242+
Value: map[string]interface{}{"one": "foo"},
243+
},
244+
},
245+
}
246+
listOfMapsTwo := ast.Variable{
247+
Type: ast.TypeList,
248+
Value: []ast.Variable{
249+
{
250+
Type: ast.TypeMap,
251+
Value: map[string]interface{}{"two": "bar"},
252+
},
253+
},
254+
}
255+
args := []interface{}{listOfMapsOne.Value, listOfMapsTwo.Value}
256+
257+
_, err := interpolationFuncConcat().Callback(args)
258+
259+
if err == nil || !strings.Contains(err.Error(), "concat() does not support lists of type map") {
260+
t.Fatalf("Expected err, got: %v", err)
261+
}
262+
}
263+
229264
func TestInterpolateFuncFile(t *testing.T) {
230265
tf, err := ioutil.TempFile("", "tf")
231266
if err != nil {

0 commit comments

Comments
 (0)