Java


JAVA.CLASS.CLONE.NF : clone not final (Java)

Summary

Method clone() is not final, which allows object-hijack.

The clone() method is used to generate a clone of an object, that is, a distinct instance with the same behavior as the original one. The default implementation in java.lang.Object checks that the object to clone either implements the java.lang.Cloneable interface or throws a java.lang.CloneNotSupportedException.

In order to clone an object it is generally sufficient to implement java.lang.Cloneable and use the default implementation of clone(). It is also possible to override that default implementation. Note that a class that inherits the default implementation allows its subclasses to implement java.lang.Cloneable and clone its instances.

Properties

Class Name clone not final (Java)
Significance reliability
Mnemonic JAVA.CLASS.CLONE.NF
Categories
CWE CWE:491 Public cloneable() Method Without Final ('Object Hijack')
CERT-Java CERT-Java:OBJ07-J Sensitive classes must not let themselves be copied
Availability Available for Java only.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="clone not final (Java)"

Resolution

Check that clone() is only redefined in classes that implement java.lang.Cloneable. Check that the default implementation of clone() is only used for classes that have explicitly declared to implement java.lang.Cloneable.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.