Hashing guarantees two objects are different. Not that they are the same
TL;DR: If you check for the hash, you should also check for equality
Problems
Solutions
- Check for hash (fast) and then check for Equality (slow)
Context
In 2022 Oct 7th one of the larger blockchains had to be halted.
This news was shocking since most blockchains are decentralized by definition.
You can read a full article here:
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--by_E26Wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--XvnZDWAZ--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/366059/69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
How a Hacker Stole $566M USD Exploiting a Code Smell
Maxi Contieri ・ Oct 8 '22
Sample Code
Wrong
public class Person {
public String name;
// Public attributes are another smell
@Override
public boolean equals(Person anotherPerson) {
return name.equals(anotherPerson.name);
}
@Override
public int hashCode() {
return (int)(Math.random()*256);
}
// This is just an example of non correlation
// When using HashMaps we can make a mistake
// and guess the object is not present in the collection
}
Right
public class Person {
public String name;
// Public attributes are another smell
@Override
public boolean equals(Person anotherPerson) {
return name.equals(anotherPerson.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
// This is just an example of non correlation
}
Detection
[X] Semi-Automatic
Many linters have rules for hash and equality redefinition.
With mutation testing, we can seed different objects with the same hash and check our tests.
Identity
Security
Conclusion
Every performance improvement has its drawbacks.
Caches and replications are notable examples.
We can (must) use them carefully.
Relations
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--by_E26Wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--XvnZDWAZ--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/366059/69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
Code Smell 150 - Equal Comparison
Maxi Contieri ・ Jul 19 '22
More Info
Disclaimer
Code Smells are just my opinion.
This will surprise some of your readers, but my primary interest is not with computer security. I am primarily interested in writing software that works as intended.
Wietse Venema
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--by_E26Wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--XvnZDWAZ--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/366059/69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20
This article is part of the CodeSmell Series.
Top comments (1)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.