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

panic during Sdump when sorting struct with private fields #108

Open zachmu opened 5 years ago

zachmu commented 5 years ago

I ran into this when running a test that uses testify, which uses Sdump to print pretty diffs. This bug was originally reported here:

https://github.com/stretchr/testify/issues/480

Here's a minimal program that reproduces the buggy behavior (also attached as .zip file):

package main

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

type s struct { f map[[1]byte]int }

func main() {

v1 := s{
    f: map[[1]byte]int{
        [1]byte{0x1}: 0,
        [1]byte{0x2}: 0,
    },
}

var spewConfig = spew.ConfigState{
    Indent:                  " ",
    DisablePointerAddresses: true,
    DisableCapacities:       true,
    SortKeys:                true,
}

spewConfig.Sdump(v1) // panic

}

Stack trace:

GOROOT=/usr/local/go #gosetup GOPATH=/Users/zachmu/liquidata/go-workspace #gosetup /usr/local/go/bin/go build -o /private/var/folders/gc/qlp9wrp95y59cbwc48pphkm00000gn/T/_go_build_spewbug_go1_ /Users/zachmu/liquidata/go-workspace/src/github.com/liquidata-inc/ld/dolt/go/spewbug.go #gosetup /private/var/folders/gc/qlp9wrp95y59cbwc48pphkm00000gn/T/_go_build_spewbug_go1_ #gosetup panic: reflect.Value.Interface: cannot return value obtained from unexported field or method

goroutine 1 [running]: reflect.valueInterface(0x10d4fc0, 0xc0000a4199, 0xa8, 0x1, 0x10d4fc0, 0xc0000a4198) /usr/local/go/src/reflect/value.go:990 +0x1bf reflect.Value.Interface(...) /usr/local/go/src/reflect/value.go:979 github.com/davecgh/go-spew/spew.valueSortLess(0x10d6c40, 0xc0000a4199, 0xb1, 0x10d6c40, 0xc0000a4198, 0xb1, 0x0) /Users/zachmu/liquidata/go-workspace/pkg/mod/github.com/davecgh/go-spew@v1.1.1/spew/common.go:315 +0x183 github.com/davecgh/go-spew/spew.(valuesSorter).Less(0xc0000d6040, 0x1, 0x0, 0x100b558) /Users/zachmu/liquidata/go-workspace/pkg/mod/github.com/davecgh/go-spew@v1.1.1/spew/common.go:328 +0xf7 sort.insertionSort(0x111bee0, 0xc0000d6040, 0x0, 0x2) /usr/local/go/src/sort/sort.go:27 +0xc4 sort.quickSort(0x111bee0, 0xc0000d6040, 0x0, 0x2, 0x4) /usr/local/go/src/sort/sort.go:209 +0x201 sort.Sort(0x111bee0, 0xc0000d6040) /usr/local/go/src/sort/sort.go:218 +0x79 github.com/davecgh/go-spew/spew.sortValues(0xc00009a330, 0x2, 0x2, 0xc0000a2100) /Users/zachmu/liquidata/go-workspace/pkg/mod/github.com/davecgh/go-spew@v1.1.1/spew/common.go:340 +0x70 github.com/davecgh/go-spew/spew.(dumpState).dump(0xc0000b5eb0, 0x10dc1e0, 0xc00009a210, 0x35) /Users/zachmu/liquidata/go-workspace/pkg/mod/github.com/davecgh/go-spew@v1.1.1/spew/dump.go:388 +0x60f github.com/davecgh/go-spew/spew.(dumpState).dump(0xc0000b5eb0, 0x10e0900, 0xc00009a210, 0x19) /Users/zachmu/liquidata/go-workspace/pkg/mod/github.com/davecgh/go-spew@v1.1.1/spew/dump.go:421 +0xdca github.com/davecgh/go-spew/spew.fdump(0xc0000a2100, 0x111b480, 0xc00009a240, 0xc0000b5f78, 0x1, 0x1) /Users/zachmu/liquidata/go-workspace/pkg/mod/github.com/davecgh/go-spew@v1.1.1/spew/dump.go:465 +0x15b github.com/davecgh/go-spew/spew.(ConfigState).Sdump(0xc0000a2100, 0xc0000b5f78, 0x1, 0x1, 0xc00006e058, 0x0) /Users/zachmu/liquidata/go-workspace/pkg/mod/github.com/davecgh/go-spew@v1.1.1/spew/config.go:281 +0x78 main.main() spewbug.go:25 +0x10b

Process finished with exit code 2

Works fine if the SortKeys option is unspecified.

spewbug.go.zip

reinerRubin commented 4 years ago

@zachmu, thanks for the minimal example!

autarch commented 1 year ago

I just encountered this via testify as well. It would be great to apply the fix in #111 or some other fix.