CWE-581 对象模型违背:仅定义了一个等式与散列码

Object Model Violation: Just One of Equals and Hashcode Defined

结构: Simple

Abstraction: Base

状态: Draft

被利用可能性: unkown


The software does not maintain equal hashcodes for equal objects.


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().


  • cwe_Nature: ChildOf cwe_CWE_ID: 573 cwe_View_ID: 1000 cwe_Ordinal: Primary

  • cwe_Nature: ChildOf cwe_CWE_ID: 697 cwe_View_ID: 1000

  • cwe_Nature: ChildOf cwe_CWE_ID: 573 cwe_View_ID: 699 cwe_Ordinal: Primary


Language: {'cwe_Name': 'Java', 'cwe_Prevalence': 'Undetermined'}


范围 影响 注释
['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.




Both Equals() and Hashcode() should be defined.


映射的分类名 ImNode ID Fit Mapped Node Name
The CERT Oracle Secure Coding Standard for Java (2011) MET09-J Classes that define an equals() method must also define a hashCode() method