[CONJ-955] ArrayIndexOutOfBoundsException when using "SELECT EXISTS" with a long string argument Created: 2022-04-06  Updated: 2022-05-13  Resolved: 2022-05-13

Status: Closed
Project: MariaDB Connector/J
Component/s: Other
Affects Version/s: 3.0.4
Fix Version/s: 3.0.5

Type: Bug Priority: Critical
Reporter: Raymond Chen Assignee: Diego Dupin
Resolution: Duplicate Votes: 0
Labels: None
Environment:

MySQL 5.7.37


Issue Links:
Duplicate
duplicates CONJ-956 ArrayIndexOutOfBoundsException when a... Closed

 Description   

When we run SQL queries with "SELECT EXISTS" with a long string parameter, it will throw exceptions like:

java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 302
	at org.mariadb.jdbc.client.impl.StandardReadableByteBuf.readShort(StandardReadableByteBuf.java:141)
	at org.mariadb.jdbc.message.server.ColumnDefinitionPacket.<init>(ColumnDefinitionPacket.java:106)
	at org.mariadb.jdbc.message.ClientMessage.readPacket(ClientMessage.java:198)
	at org.mariadb.jdbc.client.impl.StandardClient.readPacket(StandardClient.java:815)
	at org.mariadb.jdbc.client.impl.StandardClient.readResults(StandardClient.java:754)
	at org.mariadb.jdbc.client.impl.StandardClient.readResponse(StandardClient.java:673)
	at org.mariadb.jdbc.client.impl.StandardClient.execute(StandardClient.java:616)
	at org.mariadb.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:93)
	at org.mariadb.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:276)
	at org.apache.commons.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:122)
	at org.apache.commons.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:122)

Steps to reproduce the issue:
1. Create the table:

CREATE TABLE test (
  field varchar(300) NOT NULL
);

2. Java code to reproduce the issue:

    ...
    Connection conn = dataSource.getConnection();
    StringBuilder sb = new StringBuilder();
    String sQuery = "SELECT EXISTS (SELECT 1 FROM test WHERE ((field=?)))";
    for (int i = 1; i <= 300; i++) {
      sb.append("a");
      if (i < 204) {
        continue;
      }
      PreparedStatement stmt = conn.prepareStatement(sQuery);
      stmt.setString(1, sb.toString());
      try {
        ResultSet rset = stmt.executeQuery();
        rset.close();
      }
      catch (Exception e) {
        System.out.println("Error " + e.getMessage() + " when i=" + i);
        e.printStackTrace();
        break;
      }
      finally {
        stmt.close();
      }
    }
    conn.close();

Exceptions are thrown when the string size is 205.
The issue only occurs when "SELECT EXISTS" is used.



 Comments   
Comment by Diego Dupin [ 2022-05-13 ]

closing as dupplicate of CONJ-956 (sorry, it must have been the other way around) corrected with commit : https://github.com/mariadb-corporation/mariadb-connector-j/commit/e15fa25ae9fe9807b407f6f017c403feb32bc542

Generated at Thu Feb 08 03:19:32 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.