Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify rule S1948: clarify that only non-static fields are serialized. #4556

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion rules/S1948/java/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ This rule raises an issue on a non-transient and non-serializable field within a

== Why is this an issue?

By contract, fields in a `Serializable` class must themselves be either `Serializable` or `transient`.
By contract, non-static fields in a `Serializable` class must themselves be either `Serializable` or `transient`.
Even if the class is never explicitly serialized or deserialized, it is not safe to assume that this cannot happen.
For instance, under load, most J2EE application frameworks flush objects to disk.

Expand Down Expand Up @@ -94,6 +94,19 @@ public class Person implements Serializable {
}
----

Finally, static fields are out of scope for serialization, so making a field static prevents issues from being raised.

[source,java]
----
public class Person implements Serializable {
private static final long serialVersionUID = 1905122041950251207L;

private String name;

private static Logger log = getLogger(); // Compliant, static fields are not serialized
}
----

== Resources

* CWE - https://cwe.mitre.org/data/definitions/594[CWE-594 - Saving Unserializable Objects to Disk]
Expand Down
Loading