petewarden / c_hashmap

A simple string hashmap in C
http://petewarden.typepad.com/
Other
524 stars 206 forks source link

Flaw in hashmap_remove()? #6

Open a1880 opened 9 years ago

a1880 commented 9 years ago

Hi Pete, hashmap_remove() might open a gap in a sequence of entries which share the same hash value. This might lead to the situation that a hashmap_put() creates a duplicate key rather than updating an existing key. A remedy would be to insert the "in_use=0" gap at the end of a sequence of entries with the same hash.

Example:

before hashmap_remove()

entry A; hash H; in_use=1 entry B; hash H; in_use=1 entry C; hash H; in_use=1

after hashmap_remove(map, "B")

entry A; hash H; in_use=1 entry -; hash -; in_use=0 entry C; hash H; in_use=1

after hashmap_put(map, "C")

entry A; hash H; in_use=1 entry C; hash H; in_use=1 entry C; hash H; in_use=1

I could write a fixed version, but I am not yet familiar enough with github to deal with pull requests.

Greetings

Axel Kemper

ajayRam2020 commented 8 years ago

is it possible for you to paste the fix in comment section ?