CWE
Home > CWE List > CWE- Individual Dictionary Definition (1.1)  
Search by ID:

CWE-580: clone() Method Without super.clone()

Individual Definition in a New Window
clone() Method Without super.clone()
Status: Draft
Weakness ID: 580 (Weakness Variant)
Description
Summary

The software contains a clone() method that fails to call super.clone() to obtain the new object.

Potential Mitigations

Call super.clone() within your clone() method, when obtaining a new object.

Demonstrative Examples

The following two classes demonstrate a bug introduced by failing to call super.clone(). Because of the way Kibitzer implements clone(), FancyKibitzer's clone method will return an object of type Kibitzer instead of FancyKibitzer.

Java Example:
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();
...
}
}
Other Notes

All implementations of clone() should obtain the new object by calling super.clone(). If a class fails to follow this convention, a subclass's clone() method will return an object of the wrong type.

It is also a good idea to declare your clone method as final. You may not want users inheriting your class to tamper with the clone method. In some cases, you can eliminate the clone method altogether in some cases and use copy constructors.

Relationships
NatureTypeIDNameView(s) this relationship pertains toView(s)
ChildOfWeakness ClassWeakness ClassWeakness Class485Insufficient Encapsulation
Development Concepts (primary)699
Research Concepts (primary)1000
ChildOfWeakness ClassWeakness ClassWeakness Class573Failure to Follow Specification
Development Concepts699
Research Concepts1000
Applicable Platforms
Languages
Java
Time of Introduction
* Implementation
Content History
Modifications
Eric Dalci. Cigital. 2008-07-01. (External)
updated Potential_Mitigations, Time_of_Introduction
CWE Content Team. MITRE. 2008-09-08. (Internal)
updated Relationships, Other_Notes
Previous Entry Names
* Erroneous Clone Method (changed 2008-04-11)
Page Last Updated: November 24, 2008