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

Stack overflow in certain situation #107

Open
ycydsxy opened this issue Jul 21, 2021 · 2 comments
Open

Stack overflow in certain situation #107

ycydsxy opened this issue Jul 21, 2021 · 2 comments
Assignees

Comments

@ycydsxy
Copy link

ycydsxy commented Jul 21, 2021

Reproducible Example

please run in https://play.golang.org

package main

import (
	"fmt"
	"github.com/jinzhu/copier"
	"test/a"
	"test/b"
)

func main() {
	aNode := a.TreeNode{
		ID: "a1",
		Children: []a.TreeNode{
			{
				ID: "a11",
			},
		},
	}
	bNode := b.TreeNode{}
	_ = copier.Copy(&bNode, &aNode)
	fmt.Println(aNode, bNode)

}
-- go.mod --
module test
-- a/node.go --
package a

type TreeNode struct {
	ID       string
	Children []TreeNode
}
-- b/node.go --
package b

type TreeNode struct {
	ID       string
	Children []TreeNode
}

Description

The code above will result in a fatal stack overflow problem. It seems like that we cannot use recursive(such as the TreeNode) struct with the same name(even if they are in different packages). If we rename a.TreeNode to a.TreeNode1, the copier will work normally.

package main

import (
	"fmt"
	"github.com/jinzhu/copier"
	"test/a"
	"test/b"
)

func main() {
	aNode := a.TreeNode1{
		ID: "a1",
		Children: []a.TreeNode1{
			{
				ID: "a11",
			},
		},
	}
	bNode := b.TreeNode{}
	_ = copier.Copy(&bNode, &aNode)
	fmt.Println(aNode, bNode)

}
-- go.mod --
module test
-- a/node.go --
package a

type TreeNode1 struct {
	ID       string
	Children []TreeNode1
}
-- b/node.go --
package b

type TreeNode struct {
	ID       string
	Children []TreeNode
}
@TobiasYin
Copy link

I think that's a bug for golang. When I use 1.16.6 compiler, stack overflow happened. But I use 1.17rc1 compiler, It works fine.

@TobiasYin
Copy link

go 1.16.6 reflect/type.go:1590 does not check package path.

func haveIdenticalType(T, V Type, cmpTags bool) bool {
	if cmpTags {
		return T == V
	}

	if T.Name() != V.Name() || T.Kind() != V.Kind() {
		return false
	}

	return haveIdenticalUnderlyingType(T.common(), V.common(), false)
}

go 1.17 reflect/type.go:1635 fixed the bug.

func haveIdenticalType(T, V Type, cmpTags bool) bool {
	if cmpTags {
		return T == V
	}

	if T.Name() != V.Name() || T.Kind() != V.Kind() || T.PkgPath() != V.PkgPath() {
		return false
	}

	return haveIdenticalUnderlyingType(T.common(), V.common(), false)
}

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

3 participants