Difference between revisions of "Object Model Violation: Just One of equals() and hashCode() Defined"

From OWASP
Jump to: navigation, search
Line 26: Line 26:
  
 
==Related Attacks==
 
==Related Attacks==
 
[[:Category:API Abuse Attack]]
 
  
 
==Related Vulnerabilities==
 
==Related Vulnerabilities==
Line 42: Line 40:
  
 
[[Category:Code Snippet]]
 
[[Category:Code Snippet]]
 +
 +
[[Category:API Abuse]]

Revision as of 13:43, 18 July 2006

This article includes content generously donated to OWASP by Fortify.JPG.

This is a Vulnerability. To view all vulnerabilities, please see the Vulnerability Category page.


Abstract

This class overrides only one of equals() and hashCode().

Description

Java objects are expected to obey a number of invariants related to equality. One of these invariants is that equal objects must have equal hashcodes. In other words, if a.equals(b) == true then a.hashCode() == b.hashCode().

Failure to uphold this invariant 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.

Examples

The following class overrides equals() but not hashCode().

 public class halfway() {
   public boolean equals(Object obj) {
 	...
   }
 }

Related Threats

Related Attacks

Related Vulnerabilities

Related Countermeasures

Categories