util/deephash: fix collisions between different types

Updates #4883

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2022-06-21 19:50:48 -07:00
committed by Brad Fitzpatrick
parent 4f6fa3d63a
commit 8c5c87be26
2 changed files with 18 additions and 2 deletions
+12 -1
View File
@@ -127,7 +127,18 @@ func Hash(v any) (s Sum) {
h.reset()
seedOnce.Do(initSeed)
h.hashUint64(seed)
h.hashValue(reflect.ValueOf(v), false)
rv := reflect.ValueOf(v)
if rv.IsValid() {
// Always treat the Hash input as an interface (it is), including hashing
// its type, otherwise two Hash calls of different types could hash to the
// same bytes off the different types and get equivalent Sum values. This is
// the same thing that we do for reflect.Kind Interface in hashValue, but
// the initial reflect.ValueOf from an interface value effectively strips
// the interface box off so we have to do it at the top level by hand.
h.hashType(rv.Type())
h.hashValue(rv, false)
}
return h.sum()
}