Fix 32bit MurmurHash64 routines

Properly handle the tail of the data buffer, accessing the tail as an
unsigned char array instead of a uint32_t array.

Issue spotted by Peter Carmichael.
This commit is contained in:
Julien BLACHE 2010-10-23 18:36:10 +02:00
parent 65c8924898
commit 2be812e04d
2 changed files with 16 additions and 10 deletions

View File

@ -107,6 +107,7 @@ murmur_hash64(const void *key, int len, uint32_t seed)
const uint32_t m = 0x5bd1e995;
const uint32_t *data;
const unsigned char *data_tail;
uint32_t k1;
uint32_t h1;
uint32_t k2;
@ -124,12 +125,12 @@ murmur_hash64(const void *key, int len, uint32_t seed)
k1 = *data++;
k1 *= m; k1 ^= k1 >> r; k1 *= m;
h1 *= m; h1 ^= k1;
len -= 4;
k2 = *data++;
k2 *= m; k2 ^= k2 >> r; k2 *= m;
h2 *= m; h2 ^= k2;
len -= 4;
len -= 8;
}
if (len >= 4)
@ -140,14 +141,16 @@ murmur_hash64(const void *key, int len, uint32_t seed)
len -= 4;
}
data_tail = (const unsigned char *)data;
switch(len)
{
case 3:
h2 ^= (uint32_t)(data[2]) << 16;
h2 ^= (uint32_t)(data_tail[2]) << 16;
case 2:
h2 ^= (uint32_t)(data[1]) << 8;
h2 ^= (uint32_t)(data_tail[1]) << 8;
case 1:
h2 ^= (uint32_t)(data[0]);
h2 ^= (uint32_t)(data_tail[0]);
h2 *= m;
};

View File

@ -684,6 +684,7 @@ murmur_hash64(const void *key, int len, uint32_t seed)
const uint32_t m = 0x5bd1e995;
const uint32_t *data;
const unsigned char *data_tail;
uint32_t k1;
uint32_t h1;
uint32_t k2;
@ -701,12 +702,12 @@ murmur_hash64(const void *key, int len, uint32_t seed)
k1 = *data++;
k1 *= m; k1 ^= k1 >> r; k1 *= m;
h1 *= m; h1 ^= k1;
len -= 4;
k2 = *data++;
k2 *= m; k2 ^= k2 >> r; k2 *= m;
h2 *= m; h2 ^= k2;
len -= 4;
len -= 8;
}
if (len >= 4)
@ -717,14 +718,16 @@ murmur_hash64(const void *key, int len, uint32_t seed)
len -= 4;
}
data_tail = (const unsigned char *)data;
switch(len)
{
case 3:
h2 ^= (uint32_t)(data[2]) << 16;
h2 ^= (uint32_t)(data_tail[2]) << 16;
case 2:
h2 ^= (uint32_t)(data[1]) << 8;
h2 ^= (uint32_t)(data_tail[1]) << 8;
case 1:
h2 ^= (uint32_t)(data[0]);
h2 ^= (uint32_t)(data_tail[0]);
h2 *= m;
};