rspec/rules/S2390/java/rule.adoc
Marco Borgeaud 4e0e265d9e Update links to securecoding.cert.org
They redirect to https://wiki.sei.cmu.edu.
Fix broken links for open rules.
Remove broken links from closed rules.
Remove links in Java rules for CERT C rules with no obvious replacement.
Expand broken tinyurl to CERT.
2024-08-22 09:59:26 +02:00

57 lines
1.7 KiB
Plaintext

== Why is this an issue?
Referencing a static member of a subclass from its parent during class initialization, makes the code more fragile and prone to future bugs.
The execution of the program will rely heavily on the order of initialization of classes and their static members.
=== What is the potential impact?
This could create what is known as an "initialization cycle", or even a deadlock in some extreme cases.
Additionally, if the order of the static class members is changed, the behavior of the program might change.
These issues can be very hard to diagnose so it is highly recommended to avoid creating this kind of dependencies.
=== Noncompliant code example
[source,java]
----
class Parent {
static int field1 = Child.method(); // Noncompliant
static int field2 = 42;
public static void main(String[] args) {
System.out.println(Parent.field1); // will display "0" instead of "42"
}
}
class Child extends Parent {
static int method() {
return Parent.field2;
}
}
----
== Resources
* CERT - https://wiki.sei.cmu.edu/confluence/display/java/DCL00-J.+Prevent+class+initialization+cycles[DCL00-J. Prevent class initialization cycles]
* https://docs.oracle.com/javase/specs/jls/se17/html/jls-12.html#jls-12.4[Section 12.4: Initialization of Classes and Interfaces] - Java Language Specification
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove this reference to "xxx".
== Comments And Links
(visible only on this page)
=== on 07 Jun 2023, 15:18:13 Leonardo Pilastri wrote:
The rule is very limited and is mentioning the parent-child class relationship, which is actually not the root cause.
Should be reworked.
endif::env-github,rspecator-view[]