hash: make flb_hash_get to work with non-null terminated strings
@@ -71,9 +71,13 @@ static unsigned int gen_hash(const void *key, int len)
/* Handle the last few bytes of the input array */
switch (len) {
- case 3: h ^= data[2] << 16;
- case 2: h ^= data[1] << 8;
- case 1: h ^= data[0]; h *= m;
+ case 3:
+ h ^= data[2] << 16;
+ case 2:
+ h ^= data[1] << 8;
+ case 1:
+ h ^= data[0];
+ h *= m;
};
/* Do a few final mixes of the hash to ensure the last few
@@ -85,7 +89,8 @@ static unsigned int gen_hash(const void *key, int len)
return (unsigned int) h;
}
-static inline void flb_hash_entry_free(struct flb_hash *ht, struct flb_hash_entry *entry)
+static inline void flb_hash_entry_free(struct flb_hash *ht,
+ struct flb_hash_entry *entry)
{
mk_list_del(&entry->_head);
mk_list_del(&entry->_head_parent);
@@ -288,10 +293,10 @@ int flb_hash_get(struct flb_hash *ht, char *key, int key_len,
if (table->count == 1) {
entry = mk_list_entry_first(&table->chains,
- struct flb_hash_entry,
- _head);
+ struct flb_hash_entry, _head);
- if (strncmp(entry->key, key, key_len) != 0) {
+ if (entry->key_len != key_len
+ || strncmp(entry->key, key, key_len) != 0) {
entry = NULL;
}
}
@@ -304,7 +309,7 @@ int flb_hash_get(struct flb_hash *ht, char *key, int key_len,
continue;
}
- if (strcmp(entry->key, key) == 0) {
+ if (strncmp(entry->key, key, key_len) == 0) {
break;
}
@@ -391,8 +396,7 @@ int flb_hash_del(struct flb_hash *ht, char *key)
table = &ht->table[id];
if (table->count == 1) {
entry = mk_list_entry_first(&table->chains,
- struct flb_hash_entry,
- _head);
+ struct flb_hash_entry, _head);
}
else {
mk_list_foreach(head, &table->chains) {
