fix: to avoid panic on untyped nil #290

fix #290
This commit is contained in:
Nao Yonashiro 2021-09-27 10:55:37 +09:00
parent 28eaf919d5
commit 0065357ebb
6 changed files with 35 additions and 5 deletions

View File

@ -2148,3 +2148,23 @@ func TestEmptyStructInterface(t *testing.T) {
t.Fatalf("failed to encode empty struct interface. expected:[%q] but got:[%q]", expected, got)
}
}
func TestIssue290(t *testing.T) {
type Issue290 interface {
A()
}
var a struct {
A Issue290
}
expected, err := stdjson.Marshal(a)
if err != nil {
t.Fatal(err)
}
got, err := json.Marshal(a)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(expected, got) {
t.Fatalf("failed to encode non empty interface. expected = %q but got %q", expected, got)
}
}

View File

@ -194,7 +194,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if code.Flags&encoder.NonEmptyInterfaceFlags != 0 {
iface := (*nonEmptyInterface)(up)
ifacePtr = iface.ptr
typ = iface.itab.typ
if iface.itab != nil {
typ = iface.itab.typ
}
} else {
iface := (*emptyInterface)(up)
ifacePtr = iface.ptr

View File

@ -194,7 +194,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if code.Flags&encoder.NonEmptyInterfaceFlags != 0 {
iface := (*nonEmptyInterface)(up)
ifacePtr = iface.ptr
typ = iface.itab.typ
if iface.itab != nil {
typ = iface.itab.typ
}
} else {
iface := (*emptyInterface)(up)
ifacePtr = iface.ptr

View File

@ -194,7 +194,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if code.Flags&encoder.NonEmptyInterfaceFlags != 0 {
iface := (*nonEmptyInterface)(up)
ifacePtr = iface.ptr
typ = iface.itab.typ
if iface.itab != nil {
typ = iface.itab.typ
}
} else {
iface := (*emptyInterface)(up)
ifacePtr = iface.ptr

View File

@ -194,7 +194,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if code.Flags&encoder.NonEmptyInterfaceFlags != 0 {
iface := (*nonEmptyInterface)(up)
ifacePtr = iface.ptr
typ = iface.itab.typ
if iface.itab != nil {
typ = iface.itab.typ
}
} else {
iface := (*emptyInterface)(up)
ifacePtr = iface.ptr

View File

@ -194,7 +194,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if code.Flags&encoder.NonEmptyInterfaceFlags != 0 {
iface := (*nonEmptyInterface)(up)
ifacePtr = iface.ptr
typ = iface.itab.typ
if iface.itab != nil {
typ = iface.itab.typ
}
} else {
iface := (*emptyInterface)(up)
ifacePtr = iface.ptr