hash: don't use signed types during index calculation

Reported by @ITikhonov in pasis/libcouplet#10
This commit is contained in:
Dmitry Podgorny
2017-06-25 21:59:27 +03:00
parent d8528b597c
commit 704c157d24

View File

@@ -106,18 +106,17 @@ void hash_release(hash_t * const table)
/** hash a key for our table lookup */ /** hash a key for our table lookup */
static int _hash_key(hash_t *table, const char *key) static int _hash_key(hash_t *table, const char *key)
{ {
int hash = 0; unsigned hash = 0;
int shift = 0; unsigned shift = 0;
const char *c = key; const unsigned char *c = (const unsigned char *)key;
while (*c != '\0') { while (*c != 0) {
/* assume 32 bit ints */ /* assume 32 bit ints */
hash ^= ((int)*c++ << shift); hash ^= ((unsigned)*c++ << shift);
shift += 8; shift += 8;
if (shift > 24) shift = 0; if (shift > 24) shift = 0;
} }
return hash % (unsigned)table->length;
return hash % table->length;
} }
/** add a key, value pair to a hash table. /** add a key, value pair to a hash table.