1312
Potential Mitigations
Phase: Implementation
Call super.clone() within your clone() method, when obtaining a new object.
Phase: Implementation
In some cases, you can eliminate the clone method altogether and use copy constructors.
Demonstrative Examples
Example 1:
The following two classes demonstrate a bug introduced by not calling super.clone(). Because
of the way Kibitzer implements clone(), FancyKibitzer's clone method will return an object of type
Kibitzer instead of FancyKibitzer.
Example Language: Java (Bad)
public class Kibitzer {
public Object clone() throws CloneNotSupportedException {
Object returnMe = new Kibitzer();
...
}
}
public class FancyKibitzer extends Kibitzer{
public Object clone() throws CloneNotSupportedException {
Object returnMe = super.clone();
...
}
}
MemberOf Relationships
This MemberOf relationships table shows additional CWE Catgeories and Views that reference this
weakness as a member. This information is often useful in understanding where a weakness fits
within the context of external information sources.
Nature Type ID Name Page
MemberOf 1002 SFP Secondary Cluster: Unexpected Entry Points 888 2421
MemberOf 1416 Comprehensive Categorization: Resource Lifecycle
Management
1400 2545
Taxonomy Mappings
Mapped Taxonomy Name Node ID Fit Mapped Node Name
Software Fault Patterns SFP28 Unexpected access points
CWE-581: Object Model Violation: Just One of Equals and Hashcode Defined
Weakness ID : 581
Structure : Simple
Abstraction : Variant
Description
The product does not maintain equal hashcodes for equal objects.
Extended 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().
Relationships
CWE Version 4.14
CWE-581: Object Model Violation: Just One of Equals and Hashcode Defined
CWE-581: Object Model Violation: Just One of Equals and Hashcode Defined