Bug 16654 – hashOf returns different hashes for the same string value
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2016-11-01T18:08:00Z
Last change time
2017-01-16T23:24:38Z
Assigned to
nobody
Creator
dev
Comments
Comment #0 by dev — 2016-11-01T18:08:00Z
The following assert fails:
import std.stdio;
import std.conv;
void main(string[] args)
{
auto x = 1;
assert(hashOf(x.to!(string)) == hashOf(x.to!(string)));
}
This is because numeric values of the pointers to the strings are being included in the hash calculation.
Discussion: http://forum.dlang.org/post/[email protected]
Comment #1 by schveiguy — 2016-11-03T13:55:23Z
As I determined in that thread, note that core.internal.hash.hashOf does not do the same thing as object.hashOf. It hashes only the bits of the given type.
I can't think of a single valid reason for hashing the pointer and length of an array instead of the contents of the array. I don't know the right answer here, but at the very least hashOf isn't documented or understood properly.
Comment #2 by schveiguy — 2016-11-03T13:56:06Z
(In reply to Steven Schveighoffer from comment #1)
> As I determined in that thread, note that core.internal.hash.hashOf does not
> do the same thing as object.hashOf. It hashes only the bits of the given
> type.
It being object.hashOf. core.internal.hash.hashOf does a more reasonable thing.
Comment #3 by github-bugzilla — 2016-12-04T16:51:45Z