Skip to content

Commit 0b1cade

Browse files
JinZhou5042Jin Zhou
andauthored
vine: check whether hash table key is present before inserting (#4309)
* vine: check whether hash table key is present before inserting * check duplicate key when inserting --------- Co-authored-by: Jin Zhou <[email protected]>
1 parent 5344fd1 commit 0b1cade

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

dttools/src/hash_table.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,15 @@ static int hash_table_reduce_buckets(struct hash_table *h)
257257

258258
int hash_table_insert(struct hash_table *h, const char *key, const void *value)
259259
{
260+
/* Return 0 if the key already exists. */
261+
void *old_value = hash_table_lookup(h, key);
262+
if (old_value) {
263+
if (old_value != value) {
264+
notice(D_DEBUG, "key %s already exists in hash table with different value, ignoring new value.", key);
265+
}
266+
return 0;
267+
}
268+
260269
if (((float)h->size / h->bucket_count) > DEFAULT_MAX_LOAD)
261270
hash_table_double_buckets(h);
262271

@@ -274,8 +283,8 @@ int hash_table_insert(struct hash_table *h, const char *key, const void *value)
274283
* key would be included or skipped in the iteration */
275284
h->cant_iterate_yet = 1;
276285
} else {
277-
/* Key already exists, free the unused entry */
278-
notice(D_DEBUG, "key % already exists in hash table, ignoring new value.", key);
286+
/* Should be unreachable but keep for safety. */
287+
notice(D_ERROR, "Failed to insert key %s into hash table.", key);
279288
free(e->key);
280289
free(e);
281290
}

0 commit comments

Comments
 (0)