Skip to content

[Bug] panic on embedded struct with recursive #459

@letientai299

Description

@letientai299

Below is a short snippet to reproduce the bug:

package demo

import (
	stdjson "encoding/json"
	"os"
	"testing"
	"github.com/goccy/go-json"
)

type User struct {
	Name string `json:"name,omitempty"`
	Car  *Car   `json:"car,omitempty"` // won't panic if commented
}

type Car struct {
	Model string `json:"model"`
	Owner *User  `json:"owner,omitempty"`
}

func Test_JSON(t *testing.T) {
	user := User{
		Name: "Tester",
	}

	type data struct {
		User
		Age int `json:"age"`
	}

	d := data{user, 12}


	if err := stdjson.NewEncoder(os.Stdout).Encode(d); err != nil {
		t.Errorf("failed: %v", err)  // stdjson doesn't panic or err
	}

	if err := json.NewEncoder(os.Stdout).Encode(d); err != nil {
		t.Errorf("failed: %v", err)
	}
}

Output:

=== RUN   Test_JSON
{"name":"Tester","age":12}
--- FAIL: Test_JSON (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x1003212e8]

goroutine 18 [running]:
testing.tRunner.func1.2({0x1005d7ea0, 0x10083a150})
	/Users/tai/.asdf/installs/golang/1.20.5/go/src/testing/testing.go:1526 +0x278
testing.tRunner.func1()
	/Users/tai/.asdf/installs/golang/1.20.5/go/src/testing/testing.go:1529 +0x448
panic({0x1005d7ea0, 0x10083a150})
	/Users/tai/.asdf/installs/golang/1.20.5/go/src/runtime/panic.go:890 +0x258
github.com/goccy/go-json/internal/encoder.copyOpcode(0x0)
	/Users/tai/Developer/gopath/pkg/mod/github.com/goccy/[email protected]/internal/encoder/opcode.go:324 +0x38
github.com/goccy/go-json/internal/encoder.(*Compiler).linkRecursiveCode(0xc0001d64b0?, 0xc0001d66f0)
	/Users/tai/Developer/gopath/pkg/mod/github.com/goccy/[email protected]/internal/encoder/compiler.go:908 +0x190
github.com/goccy/go-json/internal/encoder.(*Compiler).codeToOpcode(0xc0001798b8?, 0xc0001d66f0, 0x1005e9f00, {0x100632810, 0xc0001d64b0})
	/Users/tai/Developer/gopath/pkg/mod/github.com/goccy/[email protected]/internal/encoder/compiler.go:894 +0x244
github.com/goccy/go-json/internal/encoder.(*Compiler).codeToOpcodeSet(0xc0001799b8?, 0x1005e9f00, {0x100632810?, 0xc0001d64b0})
	/Users/tai/Developer/gopath/pkg/mod/github.com/goccy/[email protected]/internal/encoder/compiler.go:108 +0x16c
github.com/goccy/go-json/internal/encoder.(*Compiler).compile(0x10087fc10?, 0x1005e9f00)
	/Users/tai/Developer/gopath/pkg/mod/github.com/goccy/[email protected]/internal/encoder/compiler.go:104 +0x88
github.com/goccy/go-json/internal/encoder.CompileToGetCodeSet(0xc000179bd8?, 0x1005e9f00)
	/Users/tai/Developer/gopath/pkg/mod/github.com/goccy/[email protected]/internal/encoder/compiler_race.go:33 +0x2a4
github.com/goccy/go-json.encode(0xc0001e05b0, {0x1005e9f00, 0xc00015af60})
	/Users/tai/Developer/gopath/pkg/mod/github.com/goccy/[email protected]/encode.go:226 +0x134
github.com/goccy/go-json.(*Encoder).encodeWithOption(0xc000179e00, 0xc0001e05b0, {0x1005e9f00, 0xc00015af60}, {0x0, 0x0, 0x1005e9f00?})
	/Users/tai/Developer/gopath/pkg/mod/github.com/goccy/[email protected]/encode.go:77 +0x22c
github.com/goccy/go-json.(*Encoder).EncodeWithOption(0x1005e9f00?, {0x1005e9f00, 0xc00015af60}, {0x0, 0x0, 0x0})
	/Users/tai/Developer/gopath/pkg/mod/github.com/goccy/[email protected]/encode.go:42 +0xa4
github.com/goccy/go-json.(*Encoder).Encode(...)
	/Users/tai/Developer/gopath/pkg/mod/github.com/goccy/[email protected]/encode.go:34
github.com/global-loto/ampl/internal/util/app/rest.Test_JSON(0xc000103520)
	/Users/tai/Developer/elotus/ampl/internal/util/app/rest/json_test.go:37 +0x198
testing.tRunner(0xc000103520, 0x10062e9d0)
	/Users/tai/.asdf/installs/golang/1.20.5/go/src/testing/testing.go:1576 +0x18c
created by testing.(*T).Run
	/Users/tai/.asdf/installs/golang/1.20.5/go/src/testing/testing.go:1629 +0x5e8

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions