Skip to content

Commit

Permalink
hash: make flb_hash_get to work with non-null terminated strings (#1216)
Browse files Browse the repository at this point in the history
Signed-off-by: I330716 <[email protected]>
  • Loading branch information
vlvasilev authored and edsiper committed Mar 20, 2019
1 parent aae2bef commit 09723bc
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/flb_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ static unsigned int gen_hash(const void *key, int len)
uint32_t h = seed ^ len;

/* Mix 4 bytes at a time into the hash */
const unsigned char *data = (const unsigned char *)key;
const unsigned char *data = (const unsigned char *) key;

while(len >= 4) {
uint32_t k = *(uint32_t*) data;
while (len >= 4) {
uint32_t k = *(uint32_t *) data;

k *= m;
k ^= k >> r;
Expand All @@ -69,10 +69,14 @@ 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;
switch (len) {
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
Expand All @@ -84,7 +88,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);
Expand Down Expand Up @@ -265,7 +270,7 @@ int flb_hash_add(struct flb_hash *ht, char *key, int key_len,
}

int flb_hash_get(struct flb_hash *ht, char *key, int key_len,
char **out_buf, size_t *out_size)
char **out_buf, size_t * out_size)
{
int id;
unsigned int hash;
Expand All @@ -287,10 +292,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;
}
}
Expand All @@ -303,7 +308,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;
}

Expand Down Expand Up @@ -331,7 +336,7 @@ int flb_hash_get(struct flb_hash *ht, char *key, int key_len,
* entries so the 'key' parameter is required to get an exact match.
*/
int flb_hash_get_by_id(struct flb_hash *ht, int id, char *key, char **out_buf,
size_t *out_size)
size_t * out_size)
{
struct mk_list *head;
struct flb_hash_entry *entry = NULL;
Expand Down Expand Up @@ -390,8 +395,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) {
Expand Down

0 comments on commit 09723bc

Please sign in to comment.