Wiki Markup |
---|
Programmers maycould incorrectly expect that declaring a field or variable {{final}} makes the referenced object immutable. Declaring variables that have a primitive type to be {{final}} does prevent changes to their values after initialization (otherunless than through the use of the unsupported {{sun.misc.Unsafe}} class is used). However, when the variable has a reference type, the presence of a {{final}} clause in the declaration only makes _the reference itself_ immutable. The {{final}} clause has no effect on the referenced object. Consequently, the fields of the referenced object can be mutable. For example, according to the _Java Language Specification_ \[[JLS 2005|AA. Bibliography#JLS 05]\], [Section 4§4.12.4|http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.12.4], "{{final}} Variables,": |
... if a
final
variable holds a reference to an array, then the components of the array may be changed by operations on the array, but the variable will always refer to the same array.
Similarly, a final
method parameter obtains an immutable copy of the object reference; once again. Again, this has no effect on the mutability of the referenced data.
...
In this noncompliant code example, the programmer has declared the reference to the point
instance to be final
, under the incorrect assumption that this prevents modification of the values of the instance fields x
and y
. Nevertheless, the The values of the instance fields can be changed after their initialization because the final
clause applies only to the reference to the point
instance and not the referenced object.
...
When the values of the x
and y
members must remain immutable after their initialization, they should be declared final
. However, this invalidates a set_xy()
method , as because it can no longer change the values of x
and y
.
...
With this modification, the values of the instance fields become immutable and, consequently, match the programmer's intended usage model.
...
If the class must remain mutable, another compliant solution is to provide copy functionality. This compliant solution provides a clone()
method in the final
class Point
; , avoiding the elimination of the setter method.
...
The clone()
method returns a copy of the original object that reflects the state of the original object at the moment of cloning. This new object can be used without exposing the original object; because . Because the caller holds the only reference to the newly cloned instance, the instance fields cannot be changed without the caller's cooperation. This use of the clone()
method allows the class to remain securely mutable. (See guideline "OBJ08-J. Provide mutable classes with copy functionality to allow passing instances to untrusted code safely.")
The Point
class is declared final
to prevent subclasses from overriding the clone()
method. This enables the class to be suitably used without any inadvertent modifications of the original object. This solution also complies with guideline "OBJ08-J. Provide mutable classes with copy functionality to allow passing instances to untrusted code safely."
Noncompliant Code Example (Arrays)
...
This compliant solution makes the array private
, and provides public methods to get individual items and array size. Providing direct access to the array objects themselves is safe because String
is immutable.
...
Code Block | ||
---|---|---|
| ||
private static final String[] items = {/* ... */}; public static final String[] getItems() { return items.clone(); } |
ConsequentlyAs a result, the original array values cannot be modified by a client. Note that a manual deep copy could be required when dealing with arrays of objects. This generally happens when the objects do not export a clone()
method. Refer to guideline "FIO00-J. Defensively copy mutable inputs and mutable internal components" for more information.
As before, this method provides direct access to the array objects themselves, which is safe because String
is immutable. If the array contained mutable objects, the getItems()
method could return a cloned array of cloned objects.
...
Neither the original array values nor the public
list can be modified by a client. For more details about unmodifiable wrappers, refer to guideline "SEC14-J. Provide sensitive mutable classes with unmodifiable wrappers." This solution still applies if the array contains mutable items instead of String
.
...
Guideline | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
OBJ02-J | low | probable | medium | P4 | L3 |
Related Vulnerabilities
...
Related Guidelines
MITRE CWE: CWE-607 "Public Static Final Field References Mutable Object"
...
Wiki Markup |
---|
\[[Bloch 2008|AA. Bibliography#Bloch 08]\] Item 13: Minimize the accessibility of classes and members \[[Core Java 2004|AA. Bibliography#Core Java 04]\] Chapter 6 \[[JLS 2005|AA. Bibliography#JLS 05]\] Sections [4§4.12.4 "final Variables"|http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.12.4] and [6§6.6 "Access Control"|http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6] |
...