Skip to content

Commit 94eb6cc

Browse files
committed
HASH: Fix incorrect node removal in Hash_Remove and Hash_RemoveKey
The Hash_Remove and Hash_RemoveKey functions incorrectly freed the wrong node when unlinking an entry in the linked list, causing a crash. This issue went unnoticed until now because the functions were previously unused.
1 parent 1cdf632 commit 94eb6cc

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/hash.c

+10-8
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ void *Hash_AddKey(hashtable_t *table, char *key, void *data, bucket_t *buck)
280280
void Hash_Remove(hashtable_t *table, char *name)
281281
{
282282
int bucknum = Hash_Key(name, table->numbuckets);
283-
bucket_t *buck;
283+
bucket_t *buck, *to_remove;
284284

285285
buck = table->bucket[bucknum];
286286

@@ -297,9 +297,10 @@ void Hash_Remove(hashtable_t *table, char *name)
297297
{
298298
if (!STRCMP(name, buck->next->keystring))
299299
{
300-
buck->next = buck->next->next;
301-
Q_free(buck->next->keystring);
302-
Q_free(buck->next);
300+
to_remove = buck->next;
301+
buck->next = to_remove->next;
302+
Q_free(to_remove->keystring);
303+
Q_free(to_remove);
303304
return;
304305
}
305306

@@ -344,7 +345,7 @@ void Hash_RemoveData(hashtable_t *table, char *name, void *data)
344345
void Hash_RemoveKey(hashtable_t *table, char *key)
345346
{
346347
int bucknum = ((uintptr_t) key) % table->numbuckets;
347-
bucket_t *buck;
348+
bucket_t *buck, *to_remove;
348349

349350
buck = table->bucket[bucknum];
350351

@@ -361,9 +362,10 @@ void Hash_RemoveKey(hashtable_t *table, char *key)
361362
{
362363
if (buck->next->keystring == key)
363364
{
364-
buck->next = buck->next->next;
365-
Q_free(buck->next->keystring);
366-
Q_free(buck->next);
365+
to_remove = buck->next;
366+
buck->next = to_remove->next;
367+
Q_free(to_remove->keystring);
368+
Q_free(to_remove);
367369
return;
368370
}
369371

0 commit comments

Comments
 (0)