Skip to content

Commit

Permalink
fix: correctly return tombstone when theres no free element
Browse files Browse the repository at this point in the history
The dict findDictEntry function did not correctly handle the case when,
a dict had no "free" slot but had tombstones, it would loop forever.

This fixes that by conditionally returning a tombstone when all other indexes where processed.
Fixes: #726
  • Loading branch information
liz3 committed Jan 16, 2024
1 parent 89ced84 commit eeef98c
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/vm/value.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ static DictItem *findDictEntry(DictItem *entries, int capacityMask,
Value key) {
uint32_t index = hashValue(key) & capacityMask;
DictItem *tombstone = NULL;
uint32_t count = 0;

for (;;) {
DictItem *entry = &entries[index];
Expand All @@ -88,6 +89,8 @@ static DictItem *findDictEntry(DictItem *entries, int capacityMask,
} else {
// We found a tombstone.
if (tombstone == NULL) tombstone = entry;
if (count >= (uint32_t)capacityMask)
return tombstone;
}
} else if (valuesEqual(key, entry->key)) {
// We found the key.
Expand All @@ -97,6 +100,7 @@ static DictItem *findDictEntry(DictItem *entries, int capacityMask,
// printf("%d - ", index);
index = (index + 1) & capacityMask;
// printf("%d - mask: %d\n", index, capacityMask);
count++;
}
}

Expand Down

0 comments on commit eeef98c

Please sign in to comment.