diff options
author | Joe Tsai <joetsai@digital-static.net> | 2018-03-27 14:01:19 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-27 14:01:19 -0700 |
commit | ab810a8a5d6524ae4659463790261878fe6a0718 (patch) | |
tree | 345e8b6071af97f5ec000da4ba0487c0a894329d /cmp/compare_test.go | |
parent | 7d086766fb84312590ea843913dc5b8949a485c5 (diff) | |
download | go-cmp-ab810a8a5d6524ae4659463790261878fe6a0718.tar.gz |
Check for probable infinite recursive cycles (#84)
After some hard-coded limit, check the path for recursive transformers.
Note that detecting an infinite cycle of recursive transformers
would be equivalent to solving the halting problem,
so we could misdiagnose a valid recursion as infinite.
For this reason, the check only triggers after some minimum stack size
so that correctness is not compromised for nearly all situations.
However, the threshold is low enough to trigger before the Go runtime
panics with a stack overflow (which is not recoverable).
Example panic message:
<<<
panic: recursive set of Transformers detected:
Transformer(T1, main.main.func1): complex64 => complex128
Transformer(T2, main.main.func2): complex128 => [2]float64
Transformer(T3, main.main.func3): float64 => complex64
consider using cmpopts.AcyclicTransformer
>>>
Updates #77
Diffstat (limited to 'cmp/compare_test.go')
-rw-r--r-- | cmp/compare_test.go | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/cmp/compare_test.go b/cmp/compare_test.go index 84c645b..eab0d0b 100644 --- a/cmp/compare_test.go +++ b/cmp/compare_test.go @@ -612,6 +612,22 @@ SplitString({cmp_test.StringBytes}.String)[2]: SplitBytes({cmp_test.StringBytes}.Bytes)[3][0]: -: 0x62 +: 0x42`, + }, { + x: "a\nb\nc\n", + y: "a\nb\nc\n", + opts: []cmp.Option{ + cmp.Transformer("SplitLines", func(s string) []string { return strings.Split(s, "\n") }), + }, + wantPanic: "recursive set of Transformers detected", + }, { + x: complex64(0), + y: complex64(0), + opts: []cmp.Option{ + cmp.Transformer("T1", func(x complex64) complex128 { return complex128(x) }), + cmp.Transformer("T2", func(x complex128) [2]float64 { return [2]float64{real(x), imag(x)} }), + cmp.Transformer("T3", func(x float64) complex64 { return complex64(complex(x, 0)) }), + }, + wantPanic: "recursive set of Transformers detected", }} } |