-
Notifications
You must be signed in to change notification settings - Fork 18k
hash/maphash: first call of the Reset method doesn't reset internal state #37315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
There's seems to be a bug in a Hash because if you reset and hash again you'll get the same result as the second one: package main
import (
"fmt"
"hash/maphash"
)
func main() {
h := new(maphash.Hash)
h.WriteString("hello")
fmt.Printf("%#x\n", h.Sum64()) // 0x48e0352e23362dd2
h.Reset()
h.WriteString("hello")
fmt.Printf("%#x\n", h.Sum64()) // 0xf9395b015acc3ffb
h.Reset()
h.WriteString("hello")
fmt.Printf("%#x\n", h.Sum64()) // 0xf9395b015acc3ffb
} The first hash sum seems to be ignoring all the data that was put through it and returning sum as it has an empty state: package main
import (
"fmt"
"hash/maphash"
)
func main() {
h1 := new(maphash.Hash)
h1.WriteString("hello")
sum1 := h1.Sum64() // first call on initSeed
h1.Reset()
fmt.Println(sum1 == h1.Sum64()) // true
h2 := new(maphash.Hash)
h2.SetSeed(h1.Seed())
sum2 := h2.Sum64()
fmt.Println(sum1 == sum2) // true
} I think the issue is that the first Sum64 calls |
Hash initializes seed on the first usage of seed or state with initSeed. initSeed uses SetSeed which discards accumulated data. This causes hash to return different sums for the same data in the first use and after reset. This CL fixes this issue by separating the seed set from data discard. Fixes golang#37315
Change https://golang.org/cl/220259 mentions this issue: |
Change https://golang.org/cl/220617 mentions this issue: |
…ed init Hash initializes seed on the first usage of seed or state with initSeed. initSeed uses SetSeed which discards accumulated data. This causes hash to return different sums for the same data in the first use and after reset. This CL fixes this issue by separating the seed set from data discard. Updates #37315 Change-Id: Ic7020702c2ce822eb700af462e37efab12f72054 GitHub-Last-Rev: 48b2f96 GitHub-Pull-Request: #37328 Reviewed-on: https://go-review.googlesource.com/c/go/+/220259 Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org> (cherry picked from commit 638df87) Reviewed-on: https://go-review.googlesource.com/c/go/+/220617 Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
The new
hash/maphash
package has aReset
method documented as:Moreover, the
Sum64
doc says:So it appears it would be reasonable to expect this to print two times the same value:
But it doesn't:
This is because even if the seed remains the same and "all the previously added bytes are discarded", the internal state of the Hash is maintained, even after the
h.Reset()
call.Is this the desired behaviour? If it is, the documentation should probably be amended to make it clear what to expect after a call to the
Reset
method.cc @alandonovan @randall77
The text was updated successfully, but these errors were encountered: