Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in hashmap.c #135

Open
sgraham opened this issue Oct 30, 2023 · 3 comments
Open

Error in hashmap.c #135

sgraham opened this issue Oct 30, 2023 · 3 comments

Comments

@sgraham
Copy link

sgraham commented Oct 30, 2023

I just discovered a bug in hashmap.c, which I found quite surprising as I'd been using it for quite a while and on some pretty large codebases!

I think the logic for TOMBSTONE is incorrect which can manifest with #undef when used by the preprocessor.

In particular:

  • assume two keys A and B are inserted
  • further assume A probes 10 elements, and B probes 5 elements.
  • then, B is deleted, so its key is marked with a TOMBSTONE
  • if A is then re-put: during probing, the TOMBSTONE from B's deletion will be found (https://github.com/rui314/chibicc/blob/main/hashmap.c#L92 ), and so there will be a duplicate of A added as the later entry will not be found.
  • During a later rehash() this will eventually assert(), but in the interim, incorrect values would be retrieved.

I think the only easy solution is to never re-use TOMBSTONEs when doing a get_or_insert_entry().

I put the test data that discovered this at #134 for reference (run ./chibicc -hashmap-test).

sgraham added a commit to sgraham/dyibicc that referenced this issue Oct 30, 2023
Reported upstream at rui314/chibicc#135

TOMBSTONE entries cannot be reused, or subsequent probes can be
incorrect.
sgraham added a commit to sgraham/dyibicc that referenced this issue Oct 30, 2023
Reported upstream at rui314/chibicc#135

TOMBSTONE entries cannot be reused, or subsequent probes can be
incorrect.
@rui314
Copy link
Owner

rui314 commented Oct 31, 2023

Ah, I see the problem. That's indeed a bug, and it's hard to believe I haven't noticed before. Thank you for pointing it out!

@fuhsnn
Copy link

fuhsnn commented Oct 31, 2023

Actually, you have! #53

@sgraham
Copy link
Author

sgraham commented Oct 31, 2023

Oops, I should have done some more searching first I guess!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants