JDK-6527572 : (cs) Charset.forName can throw NullPointerException when testing bug level
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic,linux
  • CPU: generic,x86
  • Submitted: 2007-02-22
  • Updated: 2011-05-17
  • Resolved: 2011-05-17
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.
Other Other Other JDK 6 JDK 7
5.0u15-revFixed 5.0u16-revFixed 5.0u17Fixed 6u10Fixed 7 b123Fixed
Related Reports
Relates :  
Description
A licensee has observed that Charset.forName may throw NullPointerException if several threads attempt to test the bug level property at the same time. The issue is the same as the NullPointerException with Selector.open that was fixed in jdk7 as 6427854.

Comments
EVALUATION http://hg.openjdk.java.net/jdk7/build/jdk/rev/01b6d147db50
25-12-2010

SUGGESTED FIX Of course, my previous comment is completely wrong, because initialization of "bugLevel" has to be delayed until after System property infrastructure has been set up. I agree with the suggested fix.... except that.... I think we can get away without a "volatile" on this variable. This is skating on thin ice, but appears to be safe here because: - redundant initialization of "bugLevel" is harmless - String is one of the very few classes that is race-safe. That is, if multiple threads access a String, even in the presence of a data race, they are guaranteed to see the same value.
13-12-2007

SUGGESTED FIX I suggest using private static final String bugLevel = ... to ensure static initialization. There is no point to lazy initialization; it seems that atBugLevel will certainly be called in every Java program. Martin
02-03-2007

EVALUATION Race condition and a data race in Charset's atBugLevel method.
22-02-2007

SUGGESTED FIX ------- Charset.java ------- *** /tmp/sccs.FhaqDt Thu Feb 22 17:51:08 2007 --- Charset.java Thu Feb 22 17:40:52 2007 *************** *** 243,249 **** /* -- Static methods -- */ ! private static String bugLevel = null; static boolean atBugLevel(String bl) { // package-private if (bugLevel == null) { --- 243,249 ---- /* -- Static methods -- */ ! private static volatile String bugLevel = null; static boolean atBugLevel(String bl) { // package-private if (bugLevel == null) { *************** *** 251,261 **** return false; java.security.PrivilegedAction pa = new GetPropertyAction("sun.nio.cs.bugLevel"); ! bugLevel = (String)AccessController.doPrivileged(pa); ! if (bugLevel == null) ! bugLevel = ""; } ! return (bugLevel != null) && bugLevel.equals(bl); } /** --- 251,260 ---- return false; java.security.PrivilegedAction pa = new GetPropertyAction("sun.nio.cs.bugLevel"); ! String value = (String)AccessController.doPrivileged(pa); ! bugLevel = (value != null) ? value : ""; } ! return bugLevel.equals(bl); } /**
22-02-2007