Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Note
titleDeprecated

This guideline has been deprecated.  It has been merged with:

06/15/2015 -- Version 1.0

Similarly, a final method parameter obtains an immutable copy of the object reference. Again, this has no effect on the mutability of the referenced data.

Noncompliant Code Example (Mutable Class, final Reference)

In this noncompliant code example, the programmer has declared the reference to the point instance to be final under the incorrect assumption that doing so prevents modification of the values of the instance fields x and y. 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 to the referenced object.

Code Block
bgColor#ffcccc
class Point {
  private int x;
  private int y;

  Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
  void set_xy(int x, int y) {
    this.x = x;
    this.y = y;
  }
  void print_xy() {
    System
Wiki Markup
When you declare a variable *{_}final{_}*, you do not want anyone to change it.  If the type of variable is *{_}primitive types{_}*, you can undoubtedly make it. Unfortunately, if the variable is  a *{_}reference to an object{_}*, the "final" stuff you think may be not final\!

h2. Noncompliant Code Example

class Test{
 
 Test(int a, int b)
{   this.a = a;   this.b = b;  }
 void set_ab(int a, int b){  this.a = a;  this.b = b; }
 }
 void print_ab()
{   System.out.println("the value ax is: " + this.ax);
      SystemSystem.out.println("the value by is: " + this.by);  }
 private int a;
 private int b;
}}
}

public class TestFinal1PointCaller {
 
   public static void main(String\[\] args) {
{           final TestPoint mytestpoint = new TestPoint(1, 2);
           mytestpoint.print_abxy();

           //now we changeChange the value of a,b.         mytestx, y
    point.set_abxy(5, 6);
           mytestpoint.print_abxy();             }
We can see that the value of a and b has been changed, which means that when you declare a reference *{_}final{_}*, it only means that the reference can not be changed but the contents it refer to can still be changed\! 
\\

h2. Compliant Solution

If you do not want to change a and b after they are initialized, the simplest approach is to declare a and b *{_}final:_* private final int a;
 private final int b;
But now you can not have setter method of a and b.

h2. Compliant Solution 2

An alternative approach is to provide the *{_}clone{_}* method in the class. When you want do something about the object, you can use *{_}clone{_}* method to get a copy of original object. Now, you can do everything to this new object, and the original one will be never changed.  public Test2
  }
}

Compliant Solution (final Fields)

When the values of the x and y instance variables must remain immutable after their initialization, they should be declared final. However, this invalidates a set_xy() method because it can no longer change the values of x and y:

Code Block
bgColor#ccccff
class Point {
  private final int x;
  private final int y;

  Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
  void print_xy() {
    System.out.println("the value x is: " + this.x);
    System.out.println("the value y is: " + this.y);
  }

  // set_xy(int x, int y) no longer possible
}

With this modification, the values of the instance variables become immutable and consequently match the programmer's intended usage model.

Compliant Solution (Provide Copy Functionality)

If the class must remain mutable, another compliant solution is to provide copy functionality. This compliant solution provides a clone() method in the class Point, avoiding the elimination of the setter method:

Code Block
bgColor#ccccff
final public class Point implements Cloneable {
  private int x;
  private int y;

  Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
  void set_xy(int x, int y) {
    this.x = x;
    this.y = y;
  }
  void print_xy() {
    System.out.println("the value x is: "+ this.x);
    System.out.println("the value y is: "+ this.y);
  }
  public Point clone() throws CloneNotSupportedException{
{      Test2Point cloned = (Test2Point) super.clone();   return
    // No need to clone x and y as they are primitives
    return cloned;
   }
}\\

h2. Risk Assessment

Using final to declare the reference to an object is a potential security risk, because the contents of the object can still be changed. 
|| Recommendation || Severity || Likelihood || Remediation Cost || Priority || Level ||
| SEC37-J | medium | likely | low | {color:red}{*}P18{*}{color} | {color:red}{*}L1{*}{color} |

h2. Automated Detection

TODO

h2. Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

h2. References

Chapter 6, Core Java™ 2 Volume I - Fundamentals, Seventh Editionpublic class PointCaller {
  public static void main(String[] args) 
      throws CloneNotSupportedException {
    Point point = new Point(1, 2);    // Is not changed in main()
    point.print_xy();
    
    // Get the copy of original object
    Point pointCopy = point.clone();
    // pointCopy now holds a unique reference to the 
    // newly cloned Point instance

    // Change the value of x,y of the copy.
    pointCopy.set_xy(5, 6);

    // Original value remains unchanged
    point.print_xy();
  }
}

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 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 OBJ04-J. Provide mutable classes with copy functionality to safely allow passing instances to untrusted code.)

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.

 

Applicability

Incorrectly assuming that final references cause the contents of the referenced object to remain mutable can result in an attacker modifying an object believed to be immutable.

Bibliography

[Bloch 2008]

Item 13, "Minimize the Accessibility of Classes and Members"

[Core Java 2004]

Chapter 6, "Interfaces and Inner Classes"

[JLS 2013]

§4.12.4, "final Variables"
§6.6, "Access Control"

 

...

Image Added Image Added Image Added