From 5252e990db5ac71d6a92c6fcbfcc9d3c845f60bd Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Tue, 23 Jan 2024 22:26:38 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20crash=20on=20empty=20array?= =?UTF-8?q?.flat=20with=20missing=20type=20info?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- llx/builtin_array.go | 4 ++++ llx/builtin_array_test.go | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 llx/builtin_array_test.go diff --git a/llx/builtin_array.go b/llx/builtin_array.go index 9280900885..c4fd4e1cc6 100644 --- a/llx/builtin_array.go +++ b/llx/builtin_array.go @@ -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 diff --git a/llx/builtin_array_test.go b/llx/builtin_array_test.go new file mode 100644 index 0000000000..46f590eac8 --- /dev/null +++ b/llx/builtin_array_test.go @@ -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) + }) +}