Skip to content

Commit ec71d6d

Browse files
authored
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.
1 parent 449e17c commit ec71d6d

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

cmp/compare_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,17 @@ using the AllowUnexported option.`, "\n"),
12761276
y: MyComposite{},
12771277
wantEqual: false,
12781278
reason: "batched diffing for empty slices and nil slices",
1279+
}, {
1280+
label: label + "/LargeMapKey",
1281+
x: map[*[]byte]int{func() *[]byte {
1282+
b := make([]byte, 1<<20, 1<<20)
1283+
return &b
1284+
}(): 0},
1285+
y: map[*[]byte]int{func() *[]byte {
1286+
b := make([]byte, 1<<20, 1<<20)
1287+
return &b
1288+
}(): 0},
1289+
reason: "printing map keys should have some verbosity limit imposed",
12791290
}}
12801291
}
12811292

cmp/report_reflect.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ func formatMapKey(v reflect.Value, disambiguate bool, ptrs *pointerReferences) s
351351
opts.PrintAddresses = disambiguate
352352
opts.AvoidStringer = disambiguate
353353
opts.QualifiedNames = disambiguate
354+
opts.VerbosityLevel = maxVerbosityPreset
355+
opts.LimitVerbosity = true
354356
s := opts.FormatValue(v, reflect.Map, ptrs).String()
355357
return strings.TrimSpace(s)
356358
}

cmp/testdata/diffs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,12 @@
10081008
+ FloatsC: nil,
10091009
}
10101010
>>> TestDiff/Reporter/EmptySlices
1011+
<<< TestDiff/Reporter/LargeMapKey
1012+
map[*[]uint8]int{
1013+
- &⟪0xdeadf00f⟫⟪ptr:0xdeadf00f, len:1048576, cap:1048576⟫{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ...}: 0,
1014+
+ &⟪0xdeadf00f⟫⟪ptr:0xdeadf00f, len:1048576, cap:1048576⟫{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ...}: 0,
1015+
}
1016+
>>> TestDiff/Reporter/LargeMapKey
10111017
<<< TestDiff/EmbeddedStruct/ParentStructA/Inequal
10121018
teststructs.ParentStructA{
10131019
privateStruct: teststructs.privateStruct{

0 commit comments

Comments
 (0)