Skip to content

Commit

Permalink
api: Fix panic when encoding nil iface ptrs to JSON
Browse files Browse the repository at this point in the history
This patch fixes a panic that occurred when encoding nil interface
pointers to JSON using the vimtype JSON encoder.
  • Loading branch information
akutz committed Aug 23, 2024
1 parent c737066 commit e602029
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
6 changes: 5 additions & 1 deletion vim25/json/discriminator.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,11 @@ func discriminatorInterfaceEncode(e *encodeState, v reflect.Value, opts encOpts)
e.discriminatorEncodeTypeName = true
newStructEncoder(v.Type())(e, v, opts)
case reflect.Ptr:
discriminatorInterfaceEncode(e, v, opts)
if v.IsZero() {
newPtrEncoder(v.Type())(e, v, opts)
} else {
discriminatorInterfaceEncode(e, v, opts)
}
default:
discriminatorValue := opts.discriminatorValueFn(v.Type())
if discriminatorValue == "" {
Expand Down
103 changes: 103 additions & 0 deletions vim25/types/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,109 @@ func TestSerialization(t *testing.T) {
assert.JSONEq(t, expected, actual)
})
}

t.Run("ConfigSpec", func(t *testing.T) {
t.Run("Encode", func(t *testing.T) {

var testCases = []struct {
name string
data any
expected string
expectPanic bool
}{
{
name: "nil ConfigSpec",
data: (*VirtualMachineConfigSpec)(nil),
expected: "null",
},
{
name: "ConfigSpec with nil OptionValue value",
data: &VirtualMachineConfigSpec{
ExtraConfig: []BaseOptionValue{
&OptionValue{
Key: "key1",
Value: nil,
},
},
},
expected: `{"_typeName":"VirtualMachineConfigSpec","extraConfig":[{"_typeName":"OptionValue","key":"key1","value":null}]}`,
},
{
name: "ConfigSpec with nil OptionValue interface value",
data: &VirtualMachineConfigSpec{
ExtraConfig: []BaseOptionValue{
&OptionValue{
Key: "key1",
Value: (any)(nil),
},
},
},
expected: `{"_typeName":"VirtualMachineConfigSpec","extraConfig":[{"_typeName":"OptionValue","key":"key1","value":null}]}`,
},
{
name: "ConfigSpec with nil element in OptionValues",
data: &VirtualMachineConfigSpec{
ExtraConfig: []BaseOptionValue{
(*OptionValue)(nil),
},
},
expected: `{"_typeName":"VirtualMachineConfigSpec","extraConfig":[null]}`,
},
{
name: "ConfigSpec with nil ToolsConfigInfo",
data: &VirtualMachineConfigSpec{
Tools: (*ToolsConfigInfo)(nil),
},
expected: `{"_typeName":"VirtualMachineConfigSpec"}`,
},
{
name: "ConfigSpec with nil vAppConfig",
data: &VirtualMachineConfigSpec{
VAppConfig: nil,
},
expected: `{"_typeName":"VirtualMachineConfigSpec"}`,
},
{
name: "ConfigSpec with nil pointer vAppConfig ",
data: &VirtualMachineConfigSpec{
VAppConfig: (*VmConfigSpec)(nil),
},
expected: `{"_typeName":"VirtualMachineConfigSpec","vAppConfig":null}`,
},
}

for i := range testCases {
tc := testCases[i]
t.Run(tc.name, func(t *testing.T) {
var w bytes.Buffer
enc := NewJSONEncoder(&w)

var panicErr any

func() {
defer func() {
panicErr = recover()
}()
if err := enc.Encode(tc.data); err != nil {
t.Fatal(err)
}
}()

if tc.expectPanic && panicErr == nil {
t.Fatalf("did not panic, w=%v", w.String())
} else if tc.expectPanic && panicErr != nil {
t.Logf("expected panic occurred: %v\n", panicErr)
} else if !tc.expectPanic && panicErr != nil {
t.Fatalf("unexpected panic occurred: %v\n", panicErr)
} else if a, e := w.String(), tc.expected+"\n"; a != e {
t.Fatalf("act=%s != exp=%s", a, e)
} else {
t.Log(a)
}
})
}
})
})
}

func TestOptionValueSerialization(t *testing.T) {
Expand Down

0 comments on commit e602029

Please sign in to comment.