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

singleton equals may lead to java.lang.ClassCastException #308

Open
catap opened this issue Oct 24, 2023 · 2 comments
Open

singleton equals may lead to java.lang.ClassCastException #308

catap opened this issue Oct 24, 2023 · 2 comments

Comments

@catap
Copy link
Contributor

catap commented Oct 24, 2023

A trivial example:

ShortSets.singleton((short) 1).equals(IntSets.singleton(1));

fails as:

java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Short (java.lang.Integer and java.lang.Short are in module java.base of loader 'bootstrap')

	at it.unimi.dsi.fastutil.shorts.ShortCollection.contains(ShortCollection.java:134)
	at it.unimi.dsi.fastutil.shorts.AbstractShortCollection.contains(AbstractShortCollection.java:82)
	at java.base/java.util.AbstractCollection.containsAll(AbstractCollection.java:309)
	at it.unimi.dsi.fastutil.shorts.AbstractShortCollection.containsAll(AbstractShortCollection.java:154)
	at it.unimi.dsi.fastutil.shorts.AbstractShortSet.equals(AbstractShortSet.java:38)
@vigna
Copy link
Owner

vigna commented Oct 25, 2023

Mmmhh. That's somehow a fault along the whole library—if you use the object-based methods, what they try to do is a class cast to the parameter of the class and then delegate to the primitive value. contains() methods should do something smarter, that is, returning false if the object is not an instance of the parameter of the class, and then trying the delegation.

Thinking of it, it is really weird nobody ever noticed this problem before, even outside this specific case. I guess the fix is to fix contains().

@catap
Copy link
Contributor Author

catap commented Oct 25, 2023

@vigna yes, it fails on all library, indeed.

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

2 participants