CWEweakness
CWE-581 Object Model Violation: Just One of Equals and Hashcode Defined
- Abstraction
- Variant
- Structure
- Simple
- Status
- Draft
- Languages
- Java
- CWE version
- 5.0
Description
The product does not maintain equal hashcodes for equal objects.
Common consequences
• Integrity, Other: Other — If this invariant is not upheld, it is likely to cause trouble if objects of this class are stored in a collection. If the objects of the class in question are used as a key in a Hashtable or if they are inserted into a Map or Set, it is critical that equal objects have equal hashcodes.
Potential mitigations
• [Implementation] Both Equals() and Hashcode() should be defined.
Detection methods
• Automated Static Analysis: Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)