jdk1.8 sun.util.locale.LocaleMatcher StringIndexOutOfBoundsException with acceptLanguage TLH

Hannes Angst hannes at angst.email
Mon Jan 7 13:05:16 UTC 2019


Hi,

I encountered an error while parsing acceptHeader languages with JDK 1.8 on OSX.

Since the involved JDK code from RT is in share, OS should not matter.

What happened:

(1) Accept-Language: tlh
(2) Locale.LanguageRange.parse("tlh") returns "tlh" and "i-klingon" as
LanguageRange.
(3) Locale.lookup -> 
    LocaleMatcher.lookupTag at 202 fails on line 231 because
rangeForRegex.length()-2 is "-1".
    

As I am not allowed to file a bug report, nor am I sure this is really a bug, I
hope I am on the right mailing list and you can help me with that.


Test-Case:
----------------------------------------------------------------------------------------
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;


@RunWith(JUnit4.class)
public class LocaleTest {
    @Test
    public void testLocaleParser() {
        String acceptLanguageHeader = "tlh";
        List<Locale> supportedLocales = Arrays.asList(Locale.GERMAN,
Locale.ENGLISH);
        Locale fallback = Locale.ENGLISH;

        Locale locale =
Locale.lookup(Locale.LanguageRange.parse(acceptLanguageHeader),
supportedLocales);

        if (locale == null) {
            locale = fallback;
        }
        Assert.assertEquals(fallback, locale);
    }
}
----------------------------------------------------------------------------------------
Output
----------------------------------------------------------------------------------------
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
	at java.lang.String.substring(String.java:1967)
	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)
	-snip-
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at
com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at
com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at
com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
----------------------------------------------------------------------------------------


I would have written this mail in Klingon, if only I was able to ;)



Cheers,


Hannes


More information about the core-libs-dev mailing list