JDK-6853689 : Charset.lookup2() method should be synchronized on internal cache.
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio.charsets
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2009-06-22
  • Updated: 2012-07-23
  • Resolved: 2009-06-23
Description
FULL PRODUCT VERSION :
all

ADDITIONAL OS VERSION INFORMATION :
not important

A DESCRIPTION OF THE PROBLEM :
If method lookup2() would be interrupted by another thread while swapping cache1 against cache2 method lookup potentially could return the wrong charset.



REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class Charset {
    ...
    private static Charset lookup2(String charsetName) {
        Object[] a;
        Charset cs;
        if ((a = cache2) != null && charsetName.equals(a[0])) { // TODO: synchronize !!!
            cache2 = cache1;
            cache1 = a;
            cs = (Charset)a[1];
        }
        else if ((cs = standardProvider.charsetForName(charsetName)) != null ||
                (cs = lookupExtendedCharset(charsetName)) != null ||
                (cs = lookupViaProviders(charsetName)) != null)
            cache(charsetName, cs);
        else
            // Only need to check the name if we didn't find a charset for it
            checkName(charsetName);
        return cs;
    }
    ...
}
---------- END SOURCE ----------

Comments
SUGGESTED FIX See attached patches from OpenJDK bugzilla.
23-07-2012

EVALUATION both cache2 and cache1 are volatile and "a" is a local variable (what happen to cache2 and cache1 after the "a=cache2" does not matter, we work on "a") no synchronization is necessary here.
23-06-2009