Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4126371

Locale.setDefault() allows null parameter. This is senseless but dangerous.

    Details

    • Subcomponent:
    • Resolved In Build:
      1.2beta4
    • CPU:
      sparc
    • OS:
      solaris_2.5
    • Verification:
      Verified

      Description



      Name: mgC56079 Date: 04/07/98


      A bit of history first:
      we have encountered this problem while developing JCK tests for Locale class.
      The following test was developed to test Locale.setDefault(null):

      1. Locale defaultLocale=Locale.getDefault(); // save original locale
      2. Locale.setDefault(null);
      3. Locale returnedLocale = Locale.getDefault();
      4. if (returnedLocale != null) {
      5. // test fails
      6. }
      7. Locale.setDefault(defaultLocale); // restore original locale
      8. // test passes

      The problem is that the system will not work correctly if the locale is set to null.
      The call on line 7 which tries to restore the original locale will fail with
      NullPointerException if there is a security manager installed (this is the case with
      JavaTest harness in sameJVM mode).
      So the test is unable to restore the original locale and thus crashes the test run
      (harness is unable to proceed if the default locale is null).

      ==== Here is the minimized example (LocaleTest.java) =====
      import java.util.*;
      public class LocaleTest {

        public static void main(String[] args) {
          System.setSecurityManager(new SM());
          Locale.setDefault(null); // no exception asises
          Locale.setDefault(Locale.US); // exception comes here
        }

      }

      class SM extends SecurityManager {
        public void checkPropertyAccess(String prop) {}
      }
      ==== Here is the output from the test (jdk1.2beta3N) =====
      % java LocaleTest
      java.lang.NullPointerException
      at java.lang.String.toLowerCase(String.java:1189)
      at java.lang.String.toLowerCase(String.java:1228)
      at java.util.PropertyPermission.getMask(PropertyPermission.java:213)
      at java.util.PropertyPermission.<init>(PropertyPermission.java:130)
      at java.util.Locale.setDefault(Locale.java:306)
      at LocaleTest.main(LocaleTest.java:7)
      ====
      Please note: this test passes with jdk1.1.x and produces no output:
      ==== Here is the output from the test (jdk1.1.5) =====
      % java LocaleTest
      ====
      So this is both a compatibility problem and a functionality problem.

      ======================================================================

        Attachments

          Activity

            People

            • Assignee:
              rgillamsunw Richard Gillam (Inactive)
              Reporter:
              mgorshen Mikhail Gorshenev (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: