Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexported map value causes go-cmp to panic #38

Closed
btmura opened this issue Sep 1, 2017 · 1 comment
Closed

Unexported map value causes go-cmp to panic #38

btmura opened this issue Sep 1, 2017 · 1 comment
Assignees
Labels

Comments

@btmura
Copy link

btmura commented Sep 1, 2017

Input:

func TestBreakCmp(t *testing.T) {

type unexported int
type Foo struct {
	m map[string]unexported
}

f1 := &Foo{m: map[string]unexported{"foo": 1, "bar": 2}} // One key will work. Multiple causes panic.
f2 := &Foo{m: map[string]unexported{"bar": 3, "baz": 4}}

// Passes!
cmp.Diff(f1, f2, cmp.Comparer(func(_, _ Foo) bool {
	return true
}))

// Panics!
cmp.Diff(f1, f2, cmp.Comparer(func(_, _ Foo) bool {
	return false
}))

}

Output:

panic: reflect.Value.Interface: cannot return value obtained from unexported field or method [recovered]
panic: ,reflect.Value.Interface: cannot return value obtained from unexported field or method

goroutine 6 [running]:
testing.tRunner.func1(0xc0420a40f0)
C:/Go/src/testing/testing.go:711 +0x2d9
panic,(0x578e40, 0x5d5660)
C:/Go/src/runtime/panic.go:491 +0x291
,reflect.valueInterface(0x578e40, 0xc0421065b0, 0xb8, ,0x1, 0x4, 0x2)
C:/Go/src/reflect/value.go:936, +0x1e4
reflect.Value.Interface(0x578e40, 0xc0421065b0,, 0xb8, 0xc0420fe3e0, 0xc042136000)
C:/Go/src/reflect/value.go,:925 +0x4b
github.com/google/go-cmp/cmp/internal/value.SortKeys(0xc042102060,, 0x2, 0x2, 0xc042102060, 0x2, ,0x2)
D:/work/go/src/github.com/google/go-cmp/cmp/internal/value/sort.go:27 +0x16d
,github.com/google/go-cmp/cmp/internal/value.formatAny(0x587e20, 0xc0420026d8, 0x1b5,, 0x1010100, 0x0, 0x25, 0x688ce0)
, D:/work/go/src/github.com/google/go-cmp/cmp/internal/value/format.go:132 +0x1a38
github.com/google/go-cmp/cmp/internal/value.formatAny,(0x590c40, 0xc0420026c0, 0x199, 0xc001010101, ,0x0, 0xc042100012, 0x9)
D:/work/go/src/github.com/google/go-cmp/cmp/internal/value/format.go:,153 +0xea0
github.com/google/go-cmp/cmp/internal/value.Format(0x590c40, 0xc0420026c0, 0x199, 0x4bf901, 0xc042100012, 0x9)
D:/work/go/src/github.com/google/go-cmp/cmp/internal/value/format.go:30 +0x6d
github.com/google/go-cmp/cmp.(*defaultReporter).Report(0xc042038c40, 0x590c40,, 0xc042002680, 0x199, 0x590c40, 0xc0420026c0, 0x199, ,0x590c00, 0xc0420028c0, 0x2, ...)
D:/work/go/src/github.com/google/go-cmp/cmp/reporter.go:,34 +0xf7
github.com/google/go-cmp/cmp.(*state).report(0xc042082310, 0x583f00,, 0x590c40, 0xc042002680, 0x199, 0x590c40, ,0xc0420026c0, 0x199)
D:/work/go/src/github.com/google/go-cmp/cmp/compare.go:492, +0xce
github.com/google/go-cmp/cmp.(*comparer).apply(0xc042070630, 0xc042082310,, 0x590c40, 0xc042002680, 0x199, 0x590c40, 0xc0420026c0,, 0x199, 0xc0420026c0)
D:/work/go/src/github.com/google/go-cmp/cmp/options.go:315, +0x103
github.com/google/go-cmp/cmp.(*state).tryOptions(0xc042082310, 0x590c40, ,0xc042002680, 0x199, 0x590c40, 0xc0420026c0, 0x199,, 0x688ce0, 0x590c40, 0x199)
D:/work/go/src/github.com/google/go-cmp/cmp/compare.go:307 +0x154
github.com/google/go-cmp/cmp.(*state).compareAny,(0xc042082310, 0x590c40, 0xc042002680, 0x199, 0x590c40,, 0xc0420026c0, 0x199)
D:/work/go/src/github.com/google/go-cmp/cmp/compare.go:202, +0x343
github.com/google/go-cmp/cmp.(*state).compareAny(0xc042082310, 0x5857e0, ,0xc042002680, 0x16, 0x5857e0, 0xc0420026c0, 0x16)
D:/work/go/src/github.com/google/go-cmp/cmp/compare.go:244 +0x1540
github.com/google/go-cmp/cmp.Equal(0x5857e0, 0xc042002680, 0x5857e0, 0xc0420026c0, 0xc042002880, 0x2, 0x2, 0x590c40)
D:/work/go/src/github.com/google/go-cmp/cmp/compare.go:86 +0xf8
github.com/google/go-cmp/cmp.Diff(0x5857e0, 0xc042002680, 0x5857e0, 0xc0420026c0, 0xc042002860, 0x2, 0x2, 0xc042093f98, 0x4f6b55)
D:/work/go/src/github.com/google/go-cmp/cmp/compare.go,:100 +0x13a

@dsnet dsnet self-assigned this Sep 1, 2017
dsnet added a commit that referenced this issue Sep 1, 2017
Avoid using reflect.Value.Interface to de-duplicate keys since
some keys may have source from an unexported field,
causing the logic to panic. Instead, just rely on the isLess function,
which is already safe to use on unexported values.

Fixes #38
@dsnet
Copy link
Collaborator

dsnet commented Sep 1, 2017

Thanks for the great succinct reproduction. This was an interesting failure, but the fix was easy.

@dsnet dsnet closed this as completed in #39 Sep 1, 2017
dsnet added a commit that referenced this issue Sep 1, 2017
Avoid using reflect.Value.Interface to de-duplicate keys since
some keys may have source from an unexported field,
causing the logic to panic. Instead, just rely on the isLess function,
which is already safe to use on unexported values.

Fixes #38
@dsnet dsnet added the bug label Sep 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants