JDK-6881442 : (reflect) Race condition in Class.getName()
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 7
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2009-09-12
  • Updated: 2012-09-28
  • Resolved: 2009-09-26
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
7 b73Fixed
From http://mail.openjdk.java.net/pipermail/core-libs-dev/2009-September/002589.html

It is believed that the java memory model allows Class.getName()
to return null.
This is one of those methods with an intentional data race.
Probably this has not been seen in practice because only
a perverse or adversarial runtime would load the "name" field
twice, out-of-order, as it seems to be allowed to.

diff --git a/src/share/classes/java/lang/Class.java
--- a/src/share/classes/java/lang/Class.java
+++ b/src/share/classes/java/lang/Class.java
@@ -565,8 +565,9 @@
      *          represented by this object.
     public String getName() {
+        String name = this.name;
         if (name == null)
-            name = getName0();
+            this.name = name = getName0();
         return name;


EVALUATION changeset for this change: http://hg.openjdk.java.net/jdk7/tl/jdk/rev/020a0fed38c9

EVALUATION A fine idea.