Skip to content

Commit 473e593

Browse files
authored
lang/funcs/transpose: Avoid crash due to map with null values (#36611)
* lang/funcs/transpose: Avoid crash on nulls in input * add changelog entry
1 parent 3481e32 commit 473e593

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: BUG FIXES
2+
body: 'lang/funcs/transpose: Avoid crash due to map with null values'
3+
time: 2025-03-03T12:57:22.400359Z
4+
custom:
5+
Issue: "36611"

internal/lang/funcs/collection.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,14 @@ var TransposeFunc = function.New(&function.Spec{
582582

583583
for it := inputMap.ElementIterator(); it.Next(); {
584584
inKey, inVal := it.Element()
585+
if inVal.IsNull() {
586+
return cty.MapValEmpty(cty.List(cty.String)), errors.New("input must not contain null list")
587+
}
585588
for iter := inVal.ElementIterator(); iter.Next(); {
586589
_, val := iter.Element()
590+
if val.IsNull() {
591+
return cty.MapValEmpty(cty.List(cty.String)), errors.New("input list must not contain null string")
592+
}
587593
if !val.Type().Equals(cty.String) {
588594
return cty.MapValEmpty(cty.List(cty.String)), errors.New("input must be a map of lists of strings")
589595
}

internal/lang/funcs/collection_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,6 +1833,37 @@ func TestTranspose(t *testing.T) {
18331833
}).WithMarks(cty.NewValueMarks("beep", "boop", "bloop")),
18341834
false,
18351835
},
1836+
{
1837+
cty.NullVal(cty.Map(cty.List(cty.String))),
1838+
cty.NilVal,
1839+
true,
1840+
},
1841+
{
1842+
cty.MapVal(map[string]cty.Value{
1843+
"test": cty.NullVal(cty.List(cty.String)),
1844+
}),
1845+
cty.NilVal,
1846+
true,
1847+
},
1848+
{
1849+
cty.MapVal(map[string]cty.Value{
1850+
"test": cty.ListVal([]cty.Value{cty.NullVal(cty.String)}),
1851+
}),
1852+
cty.NilVal,
1853+
true,
1854+
},
1855+
{
1856+
cty.UnknownVal(cty.Map(cty.List(cty.String))),
1857+
cty.UnknownVal(cty.Map(cty.List(cty.String))).RefineNotNull(),
1858+
false,
1859+
},
1860+
{
1861+
cty.MapVal(map[string]cty.Value{
1862+
"test": cty.ListVal([]cty.Value{cty.UnknownVal(cty.String)}),
1863+
}),
1864+
cty.UnknownVal(cty.Map(cty.List(cty.String))).RefineNotNull(),
1865+
false,
1866+
},
18361867
}
18371868

18381869
for _, test := range tests {

0 commit comments

Comments
 (0)