Skip to content

Commit

Permalink
Impose verbosity limit when formatting map keys (#248)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dsnet committed Nov 24, 2020
1 parent 449e17c commit ec71d6d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cmp/compare_test.go
Expand Up @@ -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",
}}
}

Expand Down
2 changes: 2 additions & 0 deletions cmp/report_reflect.go
Expand Up @@ -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)
}
Expand Down
6 changes: 6 additions & 0 deletions cmp/testdata/diffs
Expand Up @@ -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{
Expand Down

0 comments on commit ec71d6d

Please sign in to comment.