Skip to content

Possible bug in ValueType.GetHashCode() for fields smaller than 4 bytes #9746

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

Closed
tomspilman opened this issue Feb 17, 2018 · 8 comments
Closed

Comments

@tomspilman
Copy link

I found the results of GetHashCode() odd for a struct i was testing:

https://dotnetfiddle.net/BTgRga

The reason i get these results seem to be coming from this line:

https://github.com/dotnet/coreclr/blob/master/src/vm/comutilnative.cpp#L2129

It does (INT32)(fieldSize / sizeof(INT32)) and when fieldSize is less than 4 we get 0 which results in no hash contribution for the field.

While this is a valid result for GetHashCode() it doesn't seem to be the intent of the code written and may be a bug.

@danmoseley
Copy link
Member

Same in FastGetValueTypeHashCodeHelper

@tomspilman
Copy link
Author

@danmosemsft - Yes in the case of a struct that is tightly packed and is less than 4 bytes in size....
FastGetValueTypeHashCodeHelper would return 0.

@ionuttamas
Copy link
Contributor

ionuttamas commented Feb 20, 2018

@tomspilman the same issue is by replacing char with byte or bool.

@tomspilman
Copy link
Author

the same issue is by replacing char with byte or bool.

Yes it occurs with any type less than 4 bytes.

@RussKeldorph
Copy link
Contributor

@jkotas / @sergiy-k Could you triage?

@jkotas
Copy link
Member

jkotas commented Mar 20, 2018

The default ValueType hashcode is poor and has a lot of issues: #4414, #9092, ... . This is yet another one.

@msftgits msftgits transferred this issue from dotnet/coreclr Jan 31, 2020
@msftgits msftgits added this to the Future milestone Jan 31, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 26, 2020
@joperezr joperezr removed the untriaged New issue has not been triaged by the area owner label Jul 1, 2020
Copy link
Contributor

Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

This process is part of our issue cleanup automation.

@dotnet-policy-service dotnet-policy-service bot added backlog-cleanup-candidate An inactive issue that has been marked for automated closure. no-recent-activity labels Jan 1, 2025
Copy link
Contributor

This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

@dotnet-policy-service dotnet-policy-service bot removed this from the Future milestone Jan 15, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Feb 14, 2025
@dotnet-policy-service dotnet-policy-service bot removed no-recent-activity backlog-cleanup-candidate An inactive issue that has been marked for automated closure. labels Feb 14, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants