The FindBugs Blog

Monday, October 15, 2007

Thread unsafe lazy initialization

I spent some time improving the detector for thread unsafe lazy initialization.

The following code is from JDK1.6.0, sun.security.provider.certpath.KeyChecker:


private static Set supportedExts;
public Set getSupportedExtensions() {
if (supportedExts == null) {
supportedExts = new HashSet();
supportedExts.add(PKIXExtensions.KeyUsage_Id.toString());
supportedExts.add(PKIXExtensions.ExtendedKeyUsage_Id.toString());
supportedExts.add(PKIXExtensions.SubjectAlternativeName_Id.toString());
supportedExts = Collections.unmodifiableSet(supportedExts);
}
return supportedExts;
}


So if two threads called getSupportedExtensions on two different KeyCheckers, not only could one thread see an incompletely initialized set, but one of the threads could also be handled a mutable reference to the set of supported extensions, which it could then use to add or remove supported extensions.

Oops...

Bill

Restarting the blog...

OK, yeah, I got swamped and haven't touched the blog in months.

I'll try to update it more regularly.

Bill