cespare / xxhash

A Go implementation of the 64-bit xxHash algorithm (XXH64)
MIT License
1.79k stars 123 forks source link

Make it impossible to accidentally use an uninitialized Digest #65

Open cespare opened 2 years ago

cespare commented 2 years ago

I noticed a sharp edge of the API. It's tempting (especially if xxhash.Digest is embedded in a larger structure) to do this:

var d xxhash.Digest
... d.Write(...) ...
h := d.Sum64()

But this is broken: the zero value of Digest is not usable. You must call Reset first.

We should fix this, either by making the incorrect usage crash or by automatically calling Reset when an uninitialized Digest is used. Hopefully the branch is very predictable and doesn't add much cost.