aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Tsai <joetsai@digital-static.net>2020-11-24 10:26:02 -0800
committerGitHub <noreply@github.com>2020-11-24 10:26:02 -0800
commitec71d6d790538ad88c95a192fd059e11afb45b6f (patch)
treefbd3e5242869d4ca3b168066201a8014e71a1694
parent449e17c6c9daf9b0c84a35fef7d79321b9535763 (diff)
downloadgo-cmp-ec71d6d790538ad88c95a192fd059e11afb45b6f.tar.gz
Impose verbosity limit when formatting map keys (#248)
Map keys should have a sensible verbosity limit imposed, otherwise the reporter can end up printing a massive data structure that cannot reasonably fit in memory.
-rw-r--r--cmp/compare_test.go11
-rw-r--r--cmp/report_reflect.go2
-rw-r--r--cmp/testdata/diffs6
3 files changed, 19 insertions, 0 deletions
diff --git a/cmp/compare_test.go b/cmp/compare_test.go
index 13a3a4e..84f7353 100644
--- a/cmp/compare_test.go
+++ b/cmp/compare_test.go
@@ -1276,6 +1276,17 @@ using the AllowUnexported option.`, "\n"),
y: MyComposite{},
wantEqual: false,
reason: "batched diffing for empty slices and nil slices",
+ }, {
+ label: label + "/LargeMapKey",
+ x: map[*[]byte]int{func() *[]byte {
+ b := make([]byte, 1<<20, 1<<20)
+ return &b
+ }(): 0},
+ y: map[*[]byte]int{func() *[]byte {
+ b := make([]byte, 1<<20, 1<<20)
+ return &b
+ }(): 0},
+ reason: "printing map keys should have some verbosity limit imposed",
}}
}
diff --git a/cmp/report_reflect.go b/cmp/report_reflect.go
index 5b47420..33f0357 100644
--- a/cmp/report_reflect.go
+++ b/cmp/report_reflect.go
@@ -351,6 +351,8 @@ func formatMapKey(v reflect.Value, disambiguate bool, ptrs *pointerReferences) s
opts.PrintAddresses = disambiguate
opts.AvoidStringer = disambiguate
opts.QualifiedNames = disambiguate
+ opts.VerbosityLevel = maxVerbosityPreset
+ opts.LimitVerbosity = true
s := opts.FormatValue(v, reflect.Map, ptrs).String()
return strings.TrimSpace(s)
}
diff --git a/cmp/testdata/diffs b/cmp/testdata/diffs
index dee035d..81d5769 100644
--- a/cmp/testdata/diffs
+++ b/cmp/testdata/diffs
@@ -1008,6 +1008,12 @@
+ FloatsC: nil,
}
>>> TestDiff/Reporter/EmptySlices
+<<< TestDiff/Reporter/LargeMapKey
+ map[*[]uint8]int{
+- &⟪0xdeadf00f⟫⟪ptr:0xdeadf00f, len:1048576, cap:1048576⟫{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ...}: 0,
++ &⟪0xdeadf00f⟫⟪ptr:0xdeadf00f, len:1048576, cap:1048576⟫{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ...}: 0,
+ }
+>>> TestDiff/Reporter/LargeMapKey
<<< TestDiff/EmbeddedStruct/ParentStructA/Inequal
teststructs.ParentStructA{
privateStruct: teststructs.privateStruct{