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

java.util.Locale#lookup throws java.lang.StringIndexOutOfBoundsException for range having '-' as second character

    XMLWordPrintable

Details

    • b124
    • x86_64
    • linux
    • Verified

    Description

      FULL PRODUCT VERSION :
      java version "1.8.0_45"
      Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
      Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Linux REDACTED 2.6.32-431.23.3.el6.x86_64 #1 SMP Thu Jul 31 17:20:51 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      java.util.Locale#lookup throws a StringIndexOutOfBoundsException for some valid LanguageRanges, specifically LanguageRanges where the range has a "-" that isn't the 3rd character. An example LanguageRange that causes this is "i-navajo"

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      import java.util.Collections;
      import java.util.List;
      import java.util.Locale;
      import java.util.Locale.LanguageRange;

      List<LanguageRange> ranges = Locale.LanguageRange.parse("nv");
      // the next line with throw a StringIndexOutOfBoundsException
      Locale.lookup(ranges, Collections.singleton(Locale.ENGLISH));

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      A List or null. No exception should be thrown.
      ACTUAL -
      An exception was thrown. The stack trace:

      java.lang.StringIndexOutOfBoundsException: String index out of range: -1
      at java.lang.String.substring(String.java:1954)
      at sun.util.locale.LocaleMatcher.lookupTag(LocaleMatcher.java:233)
      at sun.util.locale.LocaleMatcher.lookup(LocaleMatcher.java:193)
      at java.util.Locale.lookup(Locale.java:3261)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.Collections;
      import java.util.List;
      import java.util.Locale;
      import java.util.Locale.LanguageRange;

      List<LanguageRange> ranges = Locale.LanguageRange.parse("nv");
      // the next line with throw a StringIndexOutOfBoundsException
      Locale.lookup(ranges, Collections.singleton(Locale.ENGLISH));
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The workaround is to avoid passing some LanguageRanges to Locale.lookup. They can be filtered as follows:

      List<LanguageRange> ranges = Locale.LanguageRange.parse("nv").stream()
        .filter(lr -> {
          int prefixIndex = lr.getRange().indexOf('-');
          return prefixIndex == -1 || prefixIndex == 2;
        })
        .collect(Collectors.toList());

      Attachments

        Activity

          People

            nishjain Nishit Jain
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: