Bug #41172 ProfilerEvent.pack() throws ArrayIndexOutOfBoundsException
Submitted: 2 Dec 2008 13:04 Modified: 24 May 2019 18:26
Reporter: Brian Cavalier Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:5.1.7 OS:Any
Assigned to: Alexander Soklakov CPU Architecture:Any
Tags: connectorj, java, jdbc, profiler

[2 Dec 2008 13:04] Brian Cavalier
Description:
I am using a custom ProfilerEventHandler that uses ProfilerEvent.pack() to serialize ProfilerEvents so that they can be batched and sent via JMS message.  ProfilerEvent.pack() consistently throws ArrayIndexOutOfBoundsException.  Here is the stack trace from the attached program that reproduces the problem:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
	at java.lang.System.arraycopy(Native Method)
	at com.mysql.jdbc.profiler.ProfilerEvent.writeBytes(ProfilerEvent.java:419)
	at com.mysql.jdbc.profiler.ProfilerEvent.pack(ProfilerEvent.java:379)
	at ProfilerEventTest.main(ProfilerEventTest.java:15)

It appears that the math in the first line of ProfilerEvent.pack() (ProfilerEvent.java, line 333) is inconsistent with the data types of the fields it tries to write to the byte array.  That results in it computing a final buffer length that is too short.

How to repeat:
See the attached program.

Suggested fix:
Ensure that the initial length computation matches the data types of the fields in ProfilerEvent, or use a more robust serialization approach with a dynamic buffer, such as DataOutputStream and ByteArrayOutputStream.
[2 Dec 2008 13:05] Brian Cavalier
Reproducible test case

Attachment: ProfilerEventTest.java.zip (application/zip, text), 512 bytes.

[2 Dec 2008 15:10] Tonci Grgin
Hi Brian and thanks for great report.

Verified just as described using latest c/J sources with test case attached:
Connected to 5.0.68-pb10-log
E
Time: 0,266
There was 1 error:
1) testBug17402(testsuite.simple.TestBug17402)java.lang.ArrayIndexOutOfBoundsException
	at java.lang.System.arraycopy(Native Method)
	at com.mysql.jdbc.profiler.ProfilerEvent.writeBytes(ProfilerEvent.java:419)
	at com.mysql.jdbc.profiler.ProfilerEvent.pack(ProfilerEvent.java:379)
	at testsuite.simple.TestBug17402.testBug17402(TestBug17402.java:42)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at testsuite.simple.TestBug17402.main(TestBug17402.java:56)

FAILURES!!!
Tests run: 1,  Failures: 0,  Errors: 1

Now I'm in trouble assessing importance of this bug...
[24 May 2019 18:26] Daniel So
Posted by developer:
 
Added the following entry to the Connector/J 8.0.17 and 5.1.48 changelogs:

"Calling ProfilerEvent.pack() resulted in an ArrayIndexOutOfBoundsException. It was due to a mishandling of data types, which has been corrected with this fix."