From 2a70a71dc2a2f9bc74c8ebce09462ddb44810353 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Mon, 6 Jul 2020 15:30:04 +0200 Subject: [PATCH] fix: avoid infinite loop when parsing recursive types Mark all visited types as such when walking down struct fields. Fixes #750. Updates #652. --- _test/struct53.go | 23 +++++++++++++++++++++++ interp/type.go | 2 ++ 2 files changed, 25 insertions(+) create mode 100644 _test/struct53.go diff --git a/_test/struct53.go b/_test/struct53.go new file mode 100644 index 00000000..1d97004a --- /dev/null +++ b/_test/struct53.go @@ -0,0 +1,23 @@ +package main + +import "fmt" + +type T1 struct { + P []*T +} + +type T2 struct { + P2 *T +} + +type T struct { + *T1 + S1 *T +} + +func main() { + fmt.Println(T2{}) +} + +// Output: +// {} diff --git a/interp/type.go b/interp/type.go index f08ac796..7394d109 100644 --- a/interp/type.go +++ b/interp/type.go @@ -1385,6 +1385,8 @@ func hasRecursiveStruct(t *itype, defined map[string]*itype) bool { if defined[typ.path+"/"+typ.name] != nil { return true } + defined[typ.path+"/"+typ.name] = typ + for _, f := range typ.field { if hasRecursiveStruct(f.typ, defined) { return true