From 505fd2903253bd6b8601b9b259fb1d4f26bf7b2c Mon Sep 17 00:00:00 2001 From: Leon Mika Date: Fri, 24 May 2024 04:07:53 +0000 Subject: [PATCH] Fixed a panic which was thrown when trying to dereference an nil object --- ucl/objs.go | 4 ++++ ucl/testbuiltins_test.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/ucl/objs.go b/ucl/objs.go index c978250..804ff71 100644 --- a/ucl/objs.go +++ b/ucl/objs.go @@ -161,6 +161,10 @@ func fromGoValue(v any) (object, error) { } func fromGoReflectValue(resVal reflect.Value) (object, error) { + if !resVal.IsValid() { + return nil, nil + } + switch resVal.Kind() { case reflect.Slice: return listableProxyObject{resVal}, nil diff --git a/ucl/testbuiltins_test.go b/ucl/testbuiltins_test.go index 2a44114..c8fc3e7 100644 --- a/ucl/testbuiltins_test.go +++ b/ucl/testbuiltins_test.go @@ -531,6 +531,9 @@ func TestBuiltins_Index(t *testing.T) { {desc: "go list 1", expr: `goList | index 0 This`, want: "thing 1\n"}, {desc: "go list 2", expr: `goList | index 1 This`, want: "thing 2\n"}, + {desc: "go list 3", expr: `goList | index 2`, want: "(nil)\n"}, + {desc: "go list 4", expr: `goList | index 2 This`, want: "(nil)\n"}, + {desc: "go list 5", expr: `goList | index 30`, want: "(nil)\n"}, {desc: "go struct 1", expr: `goStruct | index Alpha`, want: "foo\n"}, {desc: "go struct 2", expr: `goStruct | index Beta`, want: "bar\n"}, @@ -562,6 +565,7 @@ func TestBuiltins_Index(t *testing.T) { return []*nest{ {This: "thing 1"}, {This: "thing 2"}, + nil, }, nil }) inst.SetBuiltin("goStruct", func(ctx context.Context, args CallArgs) (any, error) {