Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] panic on embedded struct with recursive #459

Open
letientai299 opened this issue Jul 30, 2023 · 2 comments · May be fixed by #483
Open

[Bug] panic on embedded struct with recursive #459

letientai299 opened this issue Jul 30, 2023 · 2 comments · May be fixed by #483

Comments

@letientai299
Copy link

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
@surethink
Copy link

meet the same issue

NgoKimPhu added a commit to NgoKimPhu/go-json that referenced this issue Nov 3, 2023
NgoKimPhu added a commit to NgoKimPhu/go-json that referenced this issue Nov 3, 2023
@NgoKimPhu NgoKimPhu linked a pull request Nov 3, 2023 that will close this issue
NgoKimPhu added a commit to NgoKimPhu/go-json that referenced this issue Nov 3, 2023
NgoKimPhu added a commit to NgoKimPhu/go-json that referenced this issue Nov 4, 2023
NgoKimPhu added a commit to NgoKimPhu/go-json that referenced this issue Nov 4, 2023
NgoKimPhu added a commit to NgoKimPhu/go-json that referenced this issue Nov 4, 2023
@rene-xompass
Copy link

rene-xompass commented Dec 6, 2023

Is there any plan to merge the related pull request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants