StringBuilder: ArrayIndexOutOfBoundsException

旧城冷巷雨未停 提交于 2019-12-12 21:29:05

问题


We're experiencing a strange issue with WebSphere 7/ IBM JDK 6, where one of the nodes has some initialization issue.

We have some code which calls InitialContext.lookup and on this node we're getting sometimes the following exception:

Caused by: java.lang.ArrayIndexOutOfBoundsException
         at java.lang.String.getChars(String.java:666)
         at java.lang.StringBuilder.append(StringBuilder.java:207)
         at javax.naming.spi.NamingManager.getURLContext(NamingManager.java:646)
         at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:422)
         at javax.naming.InitialContext.lookup(InitialContext.java:436)
         [...]

We had a look at the source code of javax.naming.spi.NamingManager.getURLContext:

public static Context getURLContext(String schema, Hashtable<?, ?> envmt)
        throws NamingException {

    if (null == schema || 0 == schema.length() || null == envmt) {
        return null;
    }

    // obtain pkg prefixes from hashtable
    String pkgPrefixes[] = EnvironmentReader
            .getFactoryNamesFromEnvironmentAndProviderResource(envmt, null,
                    Context.URL_PKG_PREFIXES);

    for (String element : pkgPrefixes) {
        // create factory instance
        ObjectFactory factory;
        String clsName = element + "." //$NON-NLS-1$
                + schema + "." //$NON-NLS-1$
                + schema + "URLContextFactory"; //$NON-NLS-1$
    [...]

Line 646 is the enhanced for-loop, but the next statement is a String concatenation and is probably replaced with a StringBuilder by the compiler.

We did some quick unit tests on StringBuilder but couldn't provoke an ArrayIndexOutOfBoundsException.

How can an ArrayIndexOutOfBoundsException be thrown here and how can we avoid it?

Edit:

We are using the following java version:

java version "1.6.0"
Java(TM) SE Runtime Environment (build pxa6460sr9fp2ifix-20110913_02(SR9 FP2+IV03622+IZ99243))
IBM J9 VM (build 2.4, JRE 1.6.0 IBM J9 2.4 Linux amd64-64 jvmxa6460sr9-20110912_90359 (JIT enabled, AOT enabled)
J9VM - 20110912_090359
JIT  - r9_20101028_17488ifx31
GC   - 20101027_AA)
JCL  - 20110727_04

回答1:


This is a known bug of the JIT compiler of the IBM JVM. The workaround seems to be the exclusion of getChars from the JIT compilation:

-Xjit:exclude={ProgramClass.callStringGetChars*}

See IZ78413: JIT-COMPILED STRING.GETCHARS THROWS UNEXPECTED ARRAYINDEXOUTOFBO UNDSEXCEPTION for reference.




回答2:


The code above seems to return null if you pass a null environment hashtable.

This makes me wonder if you are instantiating InitialContext with an argument of some sort.

If the code above is indeed the source, passing a null environment hashtable will short-circuit getURLContext() to return null prior to the string concatenation loop.

Can you try new InitialContext() or new InitialContext(null) if you do not need to specify JNDI environment vars (e.g. use default JNDI env)?



来源:https://stackoverflow.com/questions/16059794/stringbuilder-arrayindexoutofboundsexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!