Java Reference
In-Depth Information
Noncompliant Code Example
This noncompliant code example consists of class Mutable , which allows the internal ar-
ray object to be modified:
Click here to view code image
class Mutable {
private int[] array = new int[10];
public int[] getArray() {
return array;
}
public void setArray(int[] i) {
array = i;
}
}
//...
private Mutable mutable = new Mutable();
public Mutable getMutable() {return mutable;}
An untrusted invoker may call the mutator method setArray(), and violate the ob-
ject'simmutabilityproperty.Invokingthegettermethod getArray() alsoallowsmodific-
ationoftheprivateinternalstateoftheclass.Thisclassalsoviolates The CERT ® Oracle ®
Secure Coding Standard for Java [Long 2012], “OBJ05-J. Defensively copy private
mutable class members before returning their references.”
Noncompliant Code Example
This noncompliant code example extends the Mutable class with a MutableProtector
subclass:
Click here to view code image
class MutableProtector extends Mutable {
@Override
public int[] getArray() {
return super.getArray().clone();
}
}
// ...
private Mutable mutable = new MutableProtector();
Search WWH ::




Custom Search