Openjdk array length changes

I recently saw strange behavior in my application that I cannot explain. They are very rare, so I can not reproduce them. While the place where these problems occur is changing, the general part seems to be that the array changes size after it is created (I know this is not possible, which means weird behavior).

A few examples to clearly indicate:

java.lang.StringIndexOutOfBoundsException: String index out of range: 86 at java.lang.String.checkBounds(String.java:409) at java.lang.String.<init>(String.java:577) at com.acunia.fleet.diagnostics.providers.tacho.VDOKLineInputParser.getRealDriverID(Unknown Source) 

The code that calls this:

 public String getRealDriverID(byte[] buffer) { if (buffer.length > 86 && isDriverCardInserted(buffer)) { return new String(buffer, 70, 16); } return null; } 

So, first we check that the buffer is large enough (over 86 bytes) before trying to create a string from them.

Second example:

 java.lang.ArrayIndexOutOfBoundsException: -1 at java.lang.String.lastIndexOf(String.java:1889) at java.lang.String.lastIndexOf(String.java:1835) at java.lang.String.lastIndexOf(String.java:1817) at com.acunia.service.position.nmea.comm.CommPositionProvider.isValid(Unknown Source) 

The line that raises this exception is:

 int csi = line.lastIndexOf("*"); 

I looked at the source of openjdk String.java, but could not find the error if the arrays could not suddenly start resizing after they were created.

The only link I can find may be related to the openjdk error: https://bugs.openjdk.java.net/browse/JDK-6817012 . This error was marked as โ€œnot a problemโ€, although, while reading this, I canโ€™t say if the problem is mentioned, which was mentioned as an error, or if the person who closed the error does not see why this error can cause Problems.

If anyone has encountered a similar problem, I would really like to hear about it. As it stands, the problem is too unstable to try to fix it using other versions of openjdk.

The problem was visible on:

 hardware: custom arm platform java version "1.6.0_31" OpenJDK Runtime Environment (IcedTea6 1.13.3) (6b31-1.13.3-1~deb7u1) OpenJDK Zero VM (build 23.25-b01, mixed mode) Linux 3.2.0 #1 Fri Jun 20 10:25:16 CEST 2014 armv7l GNU/Linux 
+8
java arrays linux openjdk
source share
2 answers

This is most likely a problem with bytes and system encoding. The documentation explicitly states:

The behavior of this constructor when the specified bytes are invalid in the default encoding is not specified .

You can get around this by explicitly specifying the correct encoding:

 new String(buffer, 70, 16, StandardCharsets.UTF_8) 
+4
source share

You create a new String using the byte[] array without specifying a Charset . Thus, I assume that the default character set has been changed, and you see behavior changes. I suggest always specifying the encoding explicitly. For example:

 return new String(buffer, 70, 16, "UTF-8"); 
+3
source share

All Articles