Skip to content

Commit b947a3c

Browse files
committed
fix: add explicit null check for old_table
Adding an offset to a null pointer is undefined behavior even if the pointer is not dereferenced. Clang identifies this for Mac OS.
1 parent 1aaa6d8 commit b947a3c

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/hashmap.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,21 @@ static int hashmap_rehash(struct hashmap_base *hb, size_t table_size)
179179
hb->table_size = table_size;
180180
hb->table = new_table;
181181

182-
/* Rehash */
183-
for (entry = old_table; entry < old_table + old_size; ++entry) {
184-
if (!entry->key) {
185-
continue;
186-
}
187-
new_entry = hashmap_entry_find(hb, entry->key, true);
188-
/* Failure indicates an algorithm bug */
189-
assert(new_entry != NULL);
182+
/* Rehash all existing entries */
183+
if (old_table) {
184+
for (entry = old_table; entry < old_table + old_size; ++entry) {
185+
if (!entry->key) {
186+
continue;
187+
}
188+
new_entry = hashmap_entry_find(hb, entry->key, true);
189+
/* Failure indicates an algorithm bug */
190+
assert(new_entry != NULL);
190191

191-
/* Shallow copy */
192-
*new_entry = *entry;
192+
/* Shallow copy */
193+
*new_entry = *entry;
194+
}
195+
free(old_table);
193196
}
194-
free(old_table);
195197
return 0;
196198
}
197199

0 commit comments

Comments
 (0)