hashmap: hash_funcs - make inputs unambiguous

Make sure all variable-length inputs are properly terminated or that
their length is encoded in some way. This avoids ambiguity of
adjacent inputs.

E.g., in case of a hash function taking two strings, compressing "ab"
followed by "c" is now distinct from "a" followed by "bc".
This commit is contained in:
Tom Gundersen
2015-10-05 19:21:02 +02:00
parent b826ab586c
commit 1e2527a6fe
5 changed files with 14 additions and 1 deletions
+1 -1
View File
@@ -277,7 +277,7 @@ static const struct hashmap_type_info hashmap_type_info[_HASHMAP_TYPE_MAX] = {
};
void string_hash_func(const void *p, struct siphash *state) {
siphash24_compress(p, strlen(p), state);
siphash24_compress(p, strlen(p) + 1, state);
}
int string_compare_func(const void *a, const void *b) {
+1
View File
@@ -117,6 +117,7 @@ void client_id_hash_func(const void *p, struct siphash *state) {
assert(id->length);
assert(id->data);
siphash24_compress(&id->length, sizeof(id->length), state);
siphash24_compress(id->data, id->length, state);
}
+1
View File
@@ -74,6 +74,7 @@ static void chassis_id_hash_func(const void *p, struct siphash *state) {
assert(id);
assert(id->data);
siphash24_compress(&id->length, sizeof(id->length), state);
siphash24_compress(id->data, id->length, state);
}
+5
View File
@@ -630,12 +630,17 @@ typedef struct Member {
static void member_hash_func(const void *p, struct siphash *state) {
const Member *m = p;
uint64_t arity = 1;
assert(m);
assert(m->type);
string_hash_func(m->type, state);
arity += !!m->name + !!m->interface;
uint64_hash_func(&arity, state);
if (m->name)
string_hash_func(m->name, state);
+6
View File
@@ -399,11 +399,17 @@ void dns_name_hash_func(const void *s, struct siphash *state) {
if (k > 0)
r = k;
if (r == 0)
break;
label[r] = 0;
ascii_strlower(label);
string_hash_func(label, state);
}
/* enforce that all names are terminated by the empty label */
string_hash_func("", state);
}
int dns_name_compare_func(const void *a, const void *b) {