Race condition in Class.getName()

Martin Buchholz martinrb at google.com
Sat Sep 12 00:22:50 UTC 2009


On Fri, Sep 11, 2009 at 16:48, Joe Darcy<Joe.Darcy at sun.com> wrote:
> Martin Buchholz wrote:
>>
>> 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
>> b/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;
>>     }
>>
>> Martin
>>
>
> Hello.
>
> The change looks fine to me.  Do you want a bug filed, etc.?

Yup.  I will commit as soon as I get a bug + synopsis.



More information about the core-libs-dev mailing list