Skip to content

Commit

Permalink
🐛 fix crash on empty array.flat with missing type info
Browse files Browse the repository at this point in the history
This can unfortunately still happen, where the type info is missing and
we essentially get the array indication in the type field, but nothing
else. The subsequent for loop then crashes it.

Add testing for this use-case and fix the crash by forcing the any-type
into it.

Signed-off-by: Dominik Richter <[email protected]>
  • Loading branch information
arlimus committed Jan 24, 2024
1 parent 89fb1a1 commit 5252e99
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions llx/builtin_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,10 @@ func arrayFlat(e *blockExecutor, bind *RawData, chunk *Chunk, ref uint64) (*RawD
typ := bind.Type
for typ.IsArray() {
typ = typ.Child()
if typ.NotSet() {
typ = types.Any
break
}
}

return &RawData{Type: types.Array(typ), Value: res}, 0, nil
Expand Down
23 changes: 23 additions & 0 deletions llx/builtin_array_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1

package llx

import (
"testing"

"github.com/stretchr/testify/require"
"go.mondoo.com/cnquery/v10/types"
)

func TestArrayFlat(t *testing.T) {
t.Run("empty array with missing type info", func(t *testing.T) {
res, ref, err := arrayFlat(nil, &RawData{
Type: types.ArrayLike,
Value: []any{},
}, nil, 0)
require.NoError(t, err)
require.Equal(t, uint64(0), ref)
require.Equal(t, ArrayData(nil, types.Any), res)
})
}

0 comments on commit 5252e99

Please sign in to comment.