Skip to content

Commit

Permalink
fix: avoid infinite loop when parsing recursive types
Browse files Browse the repository at this point in the history
Mark all visited types as such when walking down struct fields.

Fixes #750.
Updates #652.
  • Loading branch information
mvertes committed Jul 6, 2020
1 parent 8514444 commit 2a70a71
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
23 changes: 23 additions & 0 deletions _test/struct53.go
Original file line number Diff line number Diff line change
@@ -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:
// {<nil>}
2 changes: 2 additions & 0 deletions interp/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2a70a71

Please sign in to comment.