go-spew icon indicating copy to clipboard operation
go-spew copied to clipboard

spew fails to detect cycles in []interface{} values

Open kortschak opened this issue 7 years ago • 6 comments

Reproducer:

package main

import "github.com/davecgh/go-spew/spew"

func main() {
    r := make([]interface{}, 1)
    r[0] = r
    spew.Dump(r)
}

See kortschak/utter#5

kortschak avatar Sep 22 '16 23:09 kortschak

Fix for the slice case at kortschak/utter#7.

kortschak avatar Sep 23 '16 01:09 kortschak

Nice find! I'll take at a look at the proposed fix and get this updated accordingly.

Fixing this should also fix #44.

davecgh avatar Sep 23 '16 23:09 davecgh

Note that it doesn't fix the type t map[T]t case. I need to spend some more time thinking on that one.

kortschak avatar Sep 24 '16 00:09 kortschak

I suspect that interface pointers in general will need to be saved since it could really happen with any compound data structure that contains an interface due to them really just being a reference.

davecgh avatar Sep 24 '16 02:09 davecgh

It can only happen with pointer or pointer-like types. So for example, you do not get the effect with type T struct { F interface{} }; v := T{}; v.F = v. So this means it's just limited to maps, slices and pointers. I have a partial fix for maps, but it need mores work.

kortschak avatar Sep 24 '16 06:09 kortschak

Here is a fix for maps https://github.com/kortschak/utter/pull/8.

kortschak avatar Sep 24 '16 06:09 kortschak