~sircmpwn/koio

138cc5eda6486ac716c8f898e42d36477746c2c0 — Seth Hall 2 years ago 1e1f636 master 0.1.2
Fix an issue with hash collisions returning incorrect values.
1 files changed, 12 insertions(+), 21 deletions(-)

M lib/hashtable.c
M lib/hashtable.c => lib/hashtable.c +12 -21
@@ 20,17 20,13 @@ void *ko_hashtable_get(struct ko_hashtable *table, const char *key) {
	unsigned int hash = djb2(key);
	unsigned int bucket = hash % buckets_size;
	struct ko_hashtable_entry *entry = table->buckets[bucket];
	if (entry) {
		if (entry->hash != hash || strcmp(entry->key, key) != 0) {
			while (entry->next) {
				entry = entry->next;
				if (!entry || (entry->hash == hash &&
							strcmp(entry->key, key) == 0)) {
					break;
				}
			}
	do {
		if (!entry || (entry->hash == hash &&
					strcmp(entry->key, key) == 0)) {
			break;
		}
	}
		entry = entry->next;
	} while (entry);
	return entry ? entry->value : NULL;
}



@@ 40,18 36,13 @@ void *ko_hashtable_set(struct ko_hashtable *table, const char *key, void *value)
	struct ko_hashtable_entry *entry = table->buckets[bucket];
	struct ko_hashtable_entry *previous = NULL;

	if (entry) {
		if (entry->hash != hash || strcmp(entry->key, key) != 0) {
			while (entry->next) {
				previous = entry;
				entry = entry->next;
				if (!entry || (entry->hash == hash &&
							strcmp(entry->key, key) == 0)) {
					break;
				}
			}
	do {
		if (!entry || (entry->hash == hash && strcmp(entry->key, key) == 0)) {
			break;
		}
	}
		previous = entry;
		entry = entry->next;
	} while (entry);

	if (entry == NULL) {
		entry = calloc(1, sizeof(struct ko_hashtable_entry));