Invoking overridable methods from the readObject() method can cause the overriding method to read the state of the subclass before it is fixed. This is because the base class is deserialized first, followed by the subclass. Also see the related guidelines CON04-J. Do not call overridable methods from synchronized regions and MET07-J. Do not invoke overridable methods on the clone under construction.
This noncompliant code example invokes an overridable method from the readObject() method.
private void readObject(final ObjectInputStream stream) throws
IOException, ClassNotFoundException {
overridableMethod();
stream.defaultReadObject();
}
|
This compliant solution removes the call to the overridable method.
private void readObject(final ObjectInputStream stream) throws
IOException, ClassNotFoundException {
stream.defaultReadObject();
}
|
Invoking overridable methods from the readObject() method can lead to initialization errors.
Rule |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
|---|---|---|---|---|---|
SER39- J |
low |
probable |
medium |
P4 |
L1 |
TODO
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
\[[API 06|AA. Java References#API 06]\] \[[Bloch 08|AA. Java References#Bloch 08]\] Item 17: "Design and document for inheritance or else prohibit it" |
SER38-J. Do not serialize direct handles to system resources 14. Serialization (SER) 49. Miscellaneous (MSC)