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

Hashtable follow-up: Don't expose pointers to internals #1365

Open
zuiderkwast opened this issue Nov 27, 2024 · 0 comments
Open

Hashtable follow-up: Don't expose pointers to internals #1365

zuiderkwast opened this issue Nov 27, 2024 · 0 comments

Comments

@zuiderkwast
Copy link
Contributor

I don't think we should ever be exposing pointers into the structure. Instead, an opaque hashsetPosition can have appropriate accessor methods.

Originally posted by @JimB123 in #1186 (review)

Regarding functions like void **hashtableFindRef() and others that return or work with pointers to internal structures.

I think we need to be careful about this one. Do we really need it? One problem I see is that it doesn't pause rehashing, so the location may be reused by something else or even deallocated before a caller modifies the pointer. There's no protection at all.

I recommend changing this to return a hashsetPosition instead of a void**. Then you can have something like hashsetGetAtPosition(position) and hashsetReplaceAtPosition(position, newElement). You can use epoch information in the hashsetPosition to guarantee safety, and also perform checks like compare(ele1, ele2) to ensure that the structure is still logically correct.

Originally posted by @JimB123 in #1186 (comment)


I tried to refactor the code to achieve this, including adding an epoch counter to the hash table structure to be able to detect changes. It turned out to be a non-trivial amount of refactoring. Since it's not a regression and can be done later, I'm moving it to a follow-up issue.

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

1 participant