java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.StackTraceElement

Description

clojure.core/throw-if creates an array to call Exception.setStracktrace() without specifying the array type. This works fine when passed at least one StackTraceElement, but in the case where passed no stack trace elements (all are filtered or stack traces are being elided by the JVM), this will be an Object[] which results in a ClassCastException:

This is tricky to reproduce because it involves stack trace filtering so there is no reproducing case here.

Patch: CLJ-2048-b.patch
Prescreened by: Alex Miller

Environment

None

Activity

Show:
Alex Miller
October 21, 2016, 2:11 PM

into-array will create a typed array based on the first element of the seq it is passed, so generally you should see a StackTraceElement[] here. I think the only time this would fail is if it was passed no stack trace elements.

Alex Miller
October 21, 2016, 2:19 PM

I'd be happy to move this through screening, but the patch needs to be in the proper format (see http://dev.clojure.org/display/community/Developing+Patches).

Gerrit Jansen van Vuuren
October 21, 2016, 2:42 PM

I'm trying to reproduce this in a way that can be presented here, but I got the compile error just after doing some serious package renaming, and can't reproduce it outside of the project itself.

Gerrit Jansen van Vuuren
October 21, 2016, 2:45 PM

ok, I'll reformat the patch after reading (http://dev.clojure.org/display/community/Developing+Patches)

Gerrit Jansen van Vuuren
October 21, 2016, 3:15 PM

I've created a new patch based on the guidelines, attached as file: CLJ-2048-b.patch.

Just to summarise:
The into-array returns the correct type if its provided with a none empty sequence, but if the sequence is empty it cannot know the type and then returns an object array. Because we set the array here to a java method Exception::setStackTrace passing it an object array causes a ClassCastException. One fix is to check for an empty sequence, but a less verbose way is just to pass the type which is known as part of the call to into-array, this is what is done in the patch CLJ-2048-b.patch.

Completed

Assignee

Unassigned

Reporter

Gerrit Jansen van Vuuren

Labels

None

Approval

Ok

Patch

Code

Fix versions

Affects versions

Priority

Major
Configure