Bug #108414 Malformed packet generation for `COM_STMT_EXECUTE`
Submitted: 7 Sep 2022 13:24 Modified: 20 Dec 2022 21:11
Reporter: Seongman Yang (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:8.0.29, 8.0.30 OS:Any
Assigned to: CPU Architecture:Any
Tags: Contribution

[7 Sep 2022 13:24] Seongman Yang
Description:
Use the parameter `useServerPrepStmts=true`.

PreparedStatement parameter for a boolean value with `targetSqlType` of `BIT`, it is mapped to type `FIELD_TYPE_VAR_STRING` to generate a malformed packet.

MySQL Server versions 5.6.51, 5.7.39, and 8.0.30 have been tested with Testcontainers.

How to repeat:
Connection conn = ...; // with useServerPrepStmts=true
PreparedStatement pstmt = conn.prepareStatement("SELECT ?"); // send COM_STMT_PREPARE

pstmt.setObject(1, true, MysqlType.BIT); // maps to FIELD_TYPE_VAR_STRING; cause of the problem

ResultSet rs = pstmt.executeQuery(); // send COM_STMT_EXECUTE with malformed packet
rs.next();

// results from MySQL Server 8.0.30
rs.getBytes(1);                 // {byte[0]} []
rs.getBoolean(1);               // false
rs.getObject(1, Boolean.class); // {Boolean} false

Suggested fix:
To be submitted on GitHub.
[7 Sep 2022 19:53] OCA Admin
Contribution submitted via Github - fix. bug#108414 Malformed Packet 
(*) Contribution by Seongman Yang (Github scndry, mysql-connector-j/pull/85#issuecomment-1239796193): I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: git_patch_1048906516.txt (text/plain), 3.29 KiB.

[8 Sep 2022 9:50] MySQL Verification Team
Hello Seongman Yang,

Thank you for the report and contribution.

regards,
Umesh
[20 Dec 2022 21:11] Daniel So
Posted by developer:
 
Added the following entry to the C/J 8.0.32 changelog: 

"When using server-side prepared statements, the BIT data type was not properly handled by NativeQueryBindValue.getFieldType() and, as a result, BIT values were being mapped to FIELD_TYPE_VAR_STRING and causing malformed packets. This patch fixes this by mapping BIT values to FIELD_TYPE_TINY, so that the values will be handled correctly when the prepared statement is executed on the server. Thanks to Seongman Yang for contributing to the patch."