Skip to content

Commit dba19c6

Browse files
committed
bytes/hash: initialize all 64 bits of hash seed
Fixes #34925 Change-Id: Iadf12ca47a69b62c3f48d732b430cc85cf62a91c Reviewed-on: https://go-review.googlesource.com/c/go/+/202577 Reviewed-by: Matthew Dempsky <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 10e7bc9 commit dba19c6

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

doc/go1.14.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,19 @@ <h2 id="library">Core library</h2>
180180

181181
</dl><!-- mime -->
182182

183+
<dl id="math"><dt><a href="/pkg/math/">math</a></dt>
184+
<dd>
185+
<p><!-- CL 127458 -->
186+
The new <a href="/pkg/math/#Fma"><code>Fma</code></a> function
187+
computes <code>x*y+z</code> in floating point with no
188+
intermediate rounding of the <code>x*y</code>
189+
computation. Several architectures implement this computation
190+
using dedicated hardware instructions for additional
191+
performance.
192+
</p>
193+
194+
</dl><!-- math -->
195+
183196
<dl id="plugin"><dt><a href="/pkg/plugin/">plugin</a></dt>
184197
<dd>
185198
<p><!-- CL 191617 -->

src/bytes/hash/hash.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ func MakeSeed(s uint64) Seed {
130130
// New returns a new Hash object. Different hash objects allocated by
131131
// this function will very likely have different seeds.
132132
func New() *Hash {
133-
seed := Seed{s: uint64(runtime_fastrand())}
133+
s1 := uint64(runtime_fastrand())
134+
s2 := uint64(runtime_fastrand())
135+
seed := Seed{s: s1<<32 + s2}
134136
return &Hash{
135137
seed: seed,
136138
state: seed,

src/bytes/hash/hash_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ func TestHashBytesVsString(t *testing.T) {
6161
}
6262
}
6363

64+
func TestHashHighBytes(t *testing.T) {
65+
// See issue 34925.
66+
const N = 10
67+
m := map[uint64]struct{}{}
68+
for i := 0; i < N; i++ {
69+
h := hash.New()
70+
h.AddString("foo")
71+
m[h.Hash()>>32] = struct{}{}
72+
}
73+
if len(m) < N/2 {
74+
t.Errorf("from %d seeds, wanted at least %d different hashes; got %d", N, N/2, len(m))
75+
}
76+
}
77+
6478
// Make sure a Hash implements the hash.Hash and hash.Hash64 interfaces.
6579
var _ basehash.Hash = &hash.Hash{}
6680
var _ basehash.Hash64 = &hash.Hash{}

0 commit comments

Comments
 (0)