go icon indicating copy to clipboard operation
go copied to clipboard

circular reference causes stack overflow

Open linkxzhou opened this issue 3 years ago • 1 comments

Test Code:

package tests

import (
	"fmt"
	"testing"

	jsoniter "github.com/json-iterator/go"
)

var json2 = jsoniter.ConfigDefault

type Node struct {
	Name string `json:"name"`
	Next *Node  `json:"next"`
}

func TestJson2(t *testing.T) {
	n1 := Node{Name: "111", Next: nil}
	n2 := Node{Name: "222", Next: &n1}
	n3 := Node{Name: "333", Next: &n2}
	n1.Next = &n3

	fmt.Print("============== ")
	json2.Marshal(n1)
}

causes stack overflow:

runtime: goroutine stack exceeds 1000000000-byte limit runtime: sp=0xc0201e0348 stack=[0xc0201e0000, 0xc0401e0000] fatal error: stack overflow

runtime stack: runtime.throw(0x62c0fd, 0xe) /root/go/src/runtime/panic.go:1116 +0x72 runtime.newstack() /root/go/src/runtime/stack.go:1034 +0x6ce runtime.morestack() /root/go/src/runtime/asm_amd64.s:449 +0x8f

linkxzhou avatar Jul 26 '22 07:07 linkxzhou

Facing same issue. Any update on this? encoding/json works fine with such structs marshalling/unmarshalling.

rohsaini avatar Aug 16 '23 05:08 rohsaini