eclipse-ee4j/jaxb-ri

Running with -XX:-StackTraceInThrowable causes a index out of bounds exception

Closed this issue · 1 comments

In 2.3.3 and 3.x The classes that sets up logging relies on

Exception().getStackTrace() to return StackTraceElement[]

The problem is that when the application is started with -XX:-StackTraceInThrowable the StackTraceElement[] is empty.

So this code
return Logger.getLogger(trace[1].getClassName());

causes an indexOutOfBounds exception.

To fix this wherever the result of getStackTrace() is used the array should be checked for zero length.

eg in release 2.3.3 com.sun.xml.bind.Utlis.java

    public static Logger getClassLogger() {
        try {
            StackTraceElement[] trace = new Exception().getStackTrace();
            return Logger.getLogger(trace[1].getClassName());
        }
        catch( SecurityException e) {
            return Logger.getLogger("com.sun.xml.bind"); // use the default
        }
    }

could be

    public static Logger getClassLogger() {
        try {
            StackTraceElement[] trace = new Exception().getStackTrace();
            if (trace.length == 0) {  //this will happen if JVM is started with -XX:-StackTraceInThrowable
                return Logger.getLogger("com.sun.xml.bind"); // use the default
            }
            return Logger.getLogger(trace[1].getClassName());
        }
        catch( SecurityException e) {
            return Logger.getLogger("com.sun.xml.bind"); // use the default
        }
    }

fixed