0

Analyzing my Android app crashes, I found a weird one that happens only on Samsung Galaxy Note 3 devices. Here is the top of crash stack:

-------
java.lang.ArrayIndexOutOfBoundsException
java.lang.String.trim(String.java:1416)
-------

Does anyone know how it can be possible?

It looks like manufacturer slightly updated original version of String class.

Comment: The problem is that String object is immutable - I can not modify internal chars array or string length in any way. That's why trim() method in theory should never throw such type of exceptions: trim() method must check array bounds by itself (that it really does in default implementation).

Artem
  • 229
  • 6
  • 13

1 Answers1

0

You might need to make your own version of trim() if you want to ensure that it is 100% vendor-independent.

Here's what the Oracle JDK version of trim() looks like:

public String trim() {
    int len = value.length;
    int st = 0;
    char[] val = value;    /* avoid getfield opcode */

    while ((st < len) && (val[st] <= ' ')) {
        st++;
    }
    while ((st < len) && (val[len - 1] <= ' ')) {
        len--;
    }
    return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
}

The value in the above fragment is the char[] of the String

Ceiling Gecko
  • 3,104
  • 2
  • 24
  • 33