Unverified Commit e232038e authored by Adam Cozzette's avatar Adam Cozzette Committed by GitHub

Merge pull request #4624 from eughermann/hash-ub-fix

Fix undefined behavior in hash function.
parents 6ab3196d 72d18e3f
...@@ -406,9 +406,10 @@ typedef std::pair<const EnumDescriptor*, int> EnumIntPair; ...@@ -406,9 +406,10 @@ typedef std::pair<const EnumDescriptor*, int> EnumIntPair;
template<typename PairType> template<typename PairType>
struct PointerIntegerPairHash { struct PointerIntegerPairHash {
size_t operator()(const PairType& p) const { size_t operator()(const PairType& p) const {
// FIXME(kenton): What is the best way to compute this hash? I have static const size_t prime1 = 16777499;
// no idea! This seems a bit better than an XOR. static const size_t prime2 = 16777619;
return reinterpret_cast<intptr_t>(p.first) * ((1 << 16) - 1) + p.second; return reinterpret_cast<size_t>(p.first) * prime1 ^
static_cast<size_t>(p.second) * prime2;
} }
#ifdef _MSC_VER #ifdef _MSC_VER
...@@ -424,11 +425,10 @@ struct PointerIntegerPairHash { ...@@ -424,11 +425,10 @@ struct PointerIntegerPairHash {
struct PointerStringPairHash { struct PointerStringPairHash {
size_t operator()(const PointerStringPair& p) const { size_t operator()(const PointerStringPair& p) const {
// FIXME(kenton): What is the best way to compute this hash? I have static const size_t prime = 16777619;
// no idea! This seems a bit better than an XOR.
hash<const char*> cstring_hash; hash<const char*> cstring_hash;
return reinterpret_cast<intptr_t>(p.first) * ((1 << 16) - 1) + return reinterpret_cast<size_t>(p.first) * prime ^
cstring_hash(p.second); static_cast<size_t>(cstring_hash(p.second));
} }
#ifdef _MSC_VER #ifdef _MSC_VER
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment