davecgh / go-spew

Implements a deep pretty printer for Go data structures to aid in debugging
ISC License
5.98k stars 361 forks source link

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

Open kortschak opened 7 years ago

kortschak commented 7 years ago

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 commented 7 years ago

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

davecgh commented 7 years ago

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

Fixing this should also fix #44.

kortschak commented 7 years ago

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

davecgh commented 7 years ago

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.

kortschak commented 7 years ago

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 commented 7 years ago

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