From: Mathew Heard Date: Sat, 14 Aug 2021 15:14:41 +0000 (+1000) Subject: hash table fix X-Git-Url: https://git.tinc-vpn.org/git/browse?a=commitdiff_plain;h=ac2a1722b71e98010324ed46235e96bfa6e672f5;p=tinc hash table fix --- diff --git a/src/hash.h b/src/hash.h index 45cf6410..11d132a4 100644 --- a/src/hash.h +++ b/src/hash.h @@ -43,21 +43,21 @@ uint32_t modulo(uint32_t hash, size_t n); void hash_insert_ ## t (hash_ ##t *hash, const t *key, const void *value) { \ uint32_t i = hash_modulo_ ## t(hash_function_ ## t(key)); \ for(uint8_t f=0; f< (HASH_SEARCH_ITERATIONS - 1); f++){ \ - if(hash->values[i] == NULL || !memcmp(key, &hash->keys[i], sizeof(#t))) { \ - memcpy(&hash->keys[i], key, sizeof(#t)); \ + if(hash->values[i] == NULL || !memcmp(key, &hash->keys[i], sizeof(t))) { \ + memcpy(&hash->keys[i], key, sizeof(t)); \ hash->values[i] = value; \ return; \ } \ if(++i == n) i = 0; \ } \ /* We always pick the last slot. It's unfair. But thats life */ \ - memcpy(&hash->keys[i], key, sizeof(#t)); \ + memcpy(&hash->keys[i], key, sizeof(t)); \ hash->values[i] = value; \ } \ void *hash_search_ ## t (const hash_ ##t *hash, const t *key) { \ uint32_t i = hash_modulo_ ## t(hash_function_ ## t(key)); \ for(uint8_t f=0; fkeys[i], sizeof(#t))) { \ + if(!memcmp(key, &hash->keys[i], sizeof(t))) { \ return (void *)hash->values[i]; \ } \ if(++i == n) i = 0; \ @@ -67,7 +67,7 @@ uint32_t modulo(uint32_t hash, size_t n); void hash_delete_ ## t (hash_ ##t *hash, const t *key) { \ uint32_t i = hash_modulo_ ## t(hash_function_ ## t(key)); \ for(uint8_t f=0; fkeys[i], sizeof(#t))) { \ + if(!memcmp(key, &hash->keys[i], sizeof(t))) { \ hash->values[i] = NULL; \ return; \ } \ @@ -76,6 +76,7 @@ uint32_t modulo(uint32_t hash, size_t n); } \ void hash_clear_ ## t(hash_ ##t *hash) { \ memset(hash->values, 0, n * sizeof(*hash->values)); \ + memset(hash->keys, 0, n * sizeof(*hash->keys)); \ } diff --git a/src/subnet.c b/src/subnet.c index ffc82a69..602177ff 100644 --- a/src/subnet.c +++ b/src/subnet.c @@ -129,6 +129,9 @@ void subnet_cache_flush_table(subnet_type_t stype) { void init_subnets(void) { hash_seed = (uint32_t)rand(); + + // tables need to be cleared on startup + subnet_cache_flush_tables(); } void exit_subnets(void) {