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

Fix HashMap put/remove returned value for empty key after clear. #237

Merged
merged 2 commits into from
May 21, 2024

Conversation

bruno-roustant
Copy link
Collaborator

@bruno-roustant bruno-roustant commented May 21, 2024

While finishing to test the small fork of HPPC in Lucene, I discovered a small bug in HashMap put and remove, in the returned value, after clear was called on the map. This is a edge case clearly, but I found a bug after 14 years of HPPC :)

This PR also adds more tests, making the randomized test against JDK HashMap run even for primitive keys/values (that revealed the small bug).

@@ -131,8 +131,8 @@ public VType put(KType key, VType value) {

final int mask = this.mask;
if (Intrinsics.<KType> isEmpty(key)) {
VType previousValue = hasEmptyKey ? Intrinsics.<VType> cast(values[mask + 1]) : Intrinsics.<VType> empty();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug fix

@@ -237,6 +237,9 @@ public VType addTo(KType key, VType incrementValue)
public VType remove(KType key) {
final int mask = this.mask;
if (Intrinsics.<KType> isEmpty(key)) {
if (!hasEmptyKey) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug fix

try {
set.indexGet(set.indexOf(key2));
fail();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fail would be caught anyway. Fix it.

@@ -393,16 +421,15 @@ public void testNullKey()
}
/*! #end !*/

/*! #if ($TemplateOptions.KTypeGeneric) !*/
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this randomized test run for primitive keys/values.

Copy link
Member

@dweiss dweiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh boy... What shocks me more is not that you've found a bug - these are always there - but that it's been 14 years (!)...

Thank you!

Copy link
Member

@dweiss dweiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @bruno-roustant ! I'll do a few other cleanups and I think this warrants a release!

@bruno-roustant bruno-roustant merged commit 33203c9 into carrotsearch:master May 21, 2024
2 checks passed
@bruno-roustant bruno-roustant deleted the fixes branch May 21, 2024 19:06
@dweiss dweiss added this to the 0.10.0 milestone May 21, 2024
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

Successfully merging this pull request may close these issues.

2 participants