0

We have some code performing var1 + "\t" + var2 ... where var1 and var2 are of type String, but it is producing the following stack trace:

Caused by: java.lang.ArrayStoreException
       at java.lang.String.getChars(String.java:814) [rt.jar:1.8.0_40]
       at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:422) [rt.jar:1.8.0_40]
       at java.lang.StringBuilder.append(StringBuilder.java:136) [rt.jar:1.8.0_40]

The OS here is CentOS 6.6 with Oracle Java 1.8.0_40

How is this even possible? String.getChars is performing a System.arraycopy(...) and passing src and tgt vars typed as char[]

The only thing I could find online that remotely came close to this issue was this bug report for "Firefox for Android" https://bugzilla.mozilla.org/show_bug.cgi?id=861660 from 2013 that has no followups.

This is the actual line of code that triggers the call to StringBuilder.append:

bw.write(
  id + "\t" + effectiveTime + "\t" + active + "\t" 
  + moduleId + "\t" + lhsId + "\t" + rhsId + "\t" 
  + roleGroup + "\t" + roleId + "\t" 
  + inferredTypeId + "\t" + someId); 

All the variables here are of type String and bw is a BufferedWriter (not that I expect that to matter). Also, there are no Threads to speak of at this point in the system. Unfortunately, we cannot replicate the bug in our tests, only those of our customer.

Bombe
  • 81,643
  • 20
  • 123
  • 127
  • 3
    Can you post the actual code and the beginning of the error message (the part before "Caused by")? – Reinstate Monica -- notmaynard Sep 04 '15 at 02:05
  • It is pretty cut and dry The `ArrayStoreException` javadoc makes it really clear why this is thrown. [Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects.](http://docs.oracle.com/javase/6/docs/api/java/lang/ArrayStoreException.html) –  Sep 04 '15 at 02:07
  • Same problem here: http://stackoverflow.com/questions/9663935/java-mysql-preparedstatement-setstring-arraystoreexception JDK bug? – Thilo Sep 04 '15 at 02:08
  • 1
    @JarrodRoberson: Not so cut and dry when it happens inside of JDK code. – Thilo Sep 04 '15 at 02:08
  • The only thing I can think of is that StringBuilder is not threadsafe and if you happen to access it from multiple threads at once, weird stuff can happen. Are you? (from what you showed so far it looks like that StringBuilder is managed by the compiler, though...) – Thilo Sep 04 '15 at 02:09
  • 3
    There is no chance we can figure this out without the actual code. – durron597 Sep 04 '15 at 02:12
  • line `814` of the `String` class in `1.8.40` - `System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);` pretty cut and dry and that call is into native code with `value` and `dst` both type `Object` so `value` and/or `dst` are not the correct type of `Object` for the copy to happen –  Sep 04 '15 at 02:15
  • There are so many high rep users looking at this question, why is mine the only "no [mcve]" close vote? – durron597 Sep 04 '15 at 02:19
  • @JarrodRoberson that is why this is so strange (thanks @Thilo) - it is JDK code and the code path to the relevant code passes in char[] as src and tgt to arraycopy. – Michael J Lawley Sep 04 '15 at 04:45

0 Answers0