mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-25 19:15:34 +00:00
util/deephash: disambiguate hashing of AppendTo (#2483)
Prepend size to AppendTo output. Fixes #2443 Signed-off-by: Joe Tsai <joetsai@digital-static.net>
This commit is contained in:
parent
23ad028414
commit
d666bd8533
@ -147,8 +147,10 @@ func (h *hasher) print(v reflect.Value) (acyclic bool) {
|
|||||||
// Use AppendTo methods, if available and cheap.
|
// Use AppendTo methods, if available and cheap.
|
||||||
if v.CanAddr() && v.Type().Implements(appenderToType) {
|
if v.CanAddr() && v.Type().Implements(appenderToType) {
|
||||||
a := v.Addr().Interface().(appenderTo)
|
a := v.Addr().Interface().(appenderTo)
|
||||||
scratch := a.AppendTo(h.scratch[:0])
|
size := h.scratch[:8]
|
||||||
w.Write(scratch)
|
record := a.AppendTo(size)
|
||||||
|
binary.LittleEndian.PutUint64(record, uint64(len(record)-len(size)))
|
||||||
|
w.Write(record)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,12 @@
|
|||||||
"tailscale.com/wgengine/wgcfg"
|
"tailscale.com/wgengine/wgcfg"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type appendBytes []byte
|
||||||
|
|
||||||
|
func (p appendBytes) AppendTo(b []byte) []byte {
|
||||||
|
return append(b, p...)
|
||||||
|
}
|
||||||
|
|
||||||
func TestHash(t *testing.T) {
|
func TestHash(t *testing.T) {
|
||||||
type tuple [2]interface{}
|
type tuple [2]interface{}
|
||||||
type iface struct{ X interface{} }
|
type iface struct{ X interface{} }
|
||||||
@ -31,6 +37,8 @@ type iface struct{ X interface{
|
|||||||
in tuple
|
in tuple
|
||||||
wantEq bool
|
wantEq bool
|
||||||
}{
|
}{
|
||||||
|
{in: tuple{[]appendBytes{{}, {0, 0, 0, 0, 0, 0, 0, 1}}, []appendBytes{{}, {0, 0, 0, 0, 0, 0, 0, 1}}}, wantEq: true},
|
||||||
|
{in: tuple{[]appendBytes{{}, {0, 0, 0, 0, 0, 0, 0, 1}}, []appendBytes{{0, 0, 0, 0, 0, 0, 0, 1}, {}}}, wantEq: false},
|
||||||
{in: tuple{iface{MyBool(true)}, iface{MyBool(true)}}, wantEq: true},
|
{in: tuple{iface{MyBool(true)}, iface{MyBool(true)}}, wantEq: true},
|
||||||
{in: tuple{iface{true}, iface{MyBool(true)}}, wantEq: false},
|
{in: tuple{iface{true}, iface{MyBool(true)}}, wantEq: false},
|
||||||
{in: tuple{iface{MyHeader{}}, iface{MyHeader{}}}, wantEq: true},
|
{in: tuple{iface{MyHeader{}}, iface{MyHeader{}}}, wantEq: true},
|
||||||
|
Loading…
Reference in New Issue
Block a user