Skip to content

Commit 75a3369

Browse files
committed
Make sure that Interface() is not called on private values.
1 parent aa5f957 commit 75a3369

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

equal.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ func (t *T) isNil(obj interface{}) bool {
147147
return v.IsNil()
148148
}
149149

150+
// Returns a string representation of a Value that is sanitized based on what
151+
// we are allowed to see. Private fields can not be exposed via a call to
152+
// Interface() and trying will cause a panic, so we must use String()
153+
// in that case.
154+
func stringValue(v reflect.Value) string {
155+
if v.CanInterface() {
156+
return fmt.Sprintf("%#v", v.Interface())
157+
} else {
158+
return v.String()
159+
}
160+
}
161+
150162
// Deep comparison. This is based on golang 1.2's reflect.Equal functionality.
151163
func (t *T) deepEqual(
152164
desc string, have, want reflect.Value, ignores []string,
@@ -206,13 +218,13 @@ func (t *T) deepEqual(
206218
checkNil := func() bool {
207219
if want.IsNil() && !have.IsNil() {
208220
diffs = append(diffs, fmt.Sprintf("%s: not equal.", desc))
209-
diffs = append(diffs, fmt.Sprintf(" have: %#v", have.Interface()))
221+
diffs = append(diffs, fmt.Sprintf(" have: %s", stringValue(have)))
210222
diffs = append(diffs, " want: nil")
211223
return true
212224
} else if !want.IsNil() && have.IsNil() {
213225
diffs = append(diffs, fmt.Sprintf("%s: not equal.", desc))
214226
diffs = append(diffs, " have: nil")
215-
diffs = append(diffs, fmt.Sprintf(" want: %#v", want.Interface()))
227+
diffs = append(diffs, fmt.Sprintf(" want: %s", stringValue(want)))
216228
return true
217229
}
218230
return false
@@ -224,8 +236,8 @@ func (t *T) deepEqual(
224236
diffs = append(diffs, fmt.Sprintf(
225237
"%s: (len(have): %d, len(want): %d)",
226238
desc, have.Len(), want.Len()))
227-
diffs = append(diffs, fmt.Sprintf(" have: %#v", have.Interface()))
228-
diffs = append(diffs, fmt.Sprintf(" want: %#v", want.Interface()))
239+
diffs = append(diffs, fmt.Sprintf(" have: %s", stringValue(have)))
240+
diffs = append(diffs, fmt.Sprintf(" want: %s", stringValue(want)))
229241
return true
230242
}
231243
return false

equal_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package testlib
1616

1717
import (
18+
"bytes"
1819
"fmt"
1920
"math/rand"
2021
"os"
@@ -259,6 +260,15 @@ func TestT_EqualAndNotEqual(t *testing.T) {
259260
[]interface{}{sCust1, sCust2},
260261
[]interface{}{dCust1})
261262

263+
// Structures with private, unexported fields.
264+
sBuff1 := bytes.NewBuffer([]byte{1, 2, 3})
265+
sBuff2 := bytes.NewBuffer([]byte{1, 2, 3})
266+
dBuff1 := bytes.NewBuffer([]byte{1, 2, 3, 4})
267+
dBuff2 := bytes.NewBuffer([]byte{1})
268+
runTest(
269+
[]interface{}{sBuff1, sBuff2},
270+
[]interface{}{dBuff1, dBuff2})
271+
262272
// Structures in a slice.
263273
sCustSlice1 := []testEqualCustomStruct{sCust1, sCust2}
264274
sCustSlice2 := []testEqualCustomStruct{sCust1, sCust2}

0 commit comments

Comments
 (0)