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: Parsing structure stack overflow for loop references #696

Open
clearcodecn opened this issue Mar 20, 2024 · 0 comments
Open

BUG: Parsing structure stack overflow for loop references #696

clearcodecn opened this issue Mar 20, 2024 · 0 comments

Comments

@clearcodecn
Copy link

follow code cause stack overflow

import (
	"fmt"
	jsoniter "github.com/json-iterator/go"
	"testing"
)

type A struct {
	B *B `json:"b"`
}

type B struct {
	A *A `json:"a"`
}

func TestJSON(t *testing.T) {
	var a = A{}
	var b = B{}
	a.B = &b
	b.A = &a

	var ji = jsoniter.ConfigCompatibleWithStandardLibrary
	data, _ := ji.Marshal(a)
	fmt.Println(string(data))
}

output:

runtime: goroutine stack exceeds 1000000000-byte limit
runtime: sp=0x140206c03b0 stack=[0x140206c0000, 0x140406c0000]
fatal error: stack overflow

runtime stack:
runtime.throw({0x1050e5fbb?, 0x105aa7f00?})
	/usr/local/go/src/runtime/panic.go:1047 +0x40 fp=0x16ff2ad50 sp=0x16ff2ad20 pc=0x10476af30
runtime.newstack()
	/usr/local/go/src/runtime/stack.go:1105 +0x468 fp=0x16ff2af00 sp=0x16ff2ad50 pc=0x104785bb8
runtime.morestack()
	/usr/local/go/src/runtime/asm_arm64.s:316 +0x70 fp=0x16ff2af00 sp=0x16ff2af00 pc=0x10479fd10

...

but official package can handle this issue normally
code:

import (
	gojson "encoding/json"
	"fmt"
	"testing"
)

type A struct {
	B *B `json:"b"`
}

type B struct {
	A *A `json:"a"`
}

func TestJSON(t *testing.T) {
	var a = A{}
	var b = B{}
	a.B = &b
	b.A = &a

	data, _ := gojson.Marshal(a)
	fmt.Println(string(data))
}

out:

=== RUN   TestJSON

--- PASS: TestJSON (0.00s)
PASS
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

No branches or pull requests

1 participant