Bug #96280 prepared cursor failed to fetch/decode result of varbinary columns
Submitted: 23 Jul 2019 4:27 Modified: 21 Oct 2022 21:14
Reporter: zy t Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / Python Severity:S3 (Non-critical)
Version:since 8.0.11, 8.0.17 OS:Any
Assigned to: CPU Architecture:Any

[23 Jul 2019 4:27] zy t
Description:
since mysql-connector-python 8.0.11 (up to 8.0.17), when fetching result from a prepared  cursor, no matter use_pure or not, it may fail if the varbinary column contains bytes cannot be decoded.

It's not the same bug with https://bugs.mysql.com/bug.php?id=94944

How to repeat:
1. create a table with varbinary column:
create table test_binary(bstr varbinary(200));

2. run following python script:

import logging
import mysql.connector

connect_param = {
	"host": "127.0.0.1",
	"port": 3306,
	"user": "root",
	"password": "",
	"database": "test",
}

conn = None

try:
	use_pure = True
	conn = mysql.connector.connect(**connect_param, use_pure=use_pure)

	params = list()
	params.append(
		b'\x0A\x17\x0A\x0B\x31\x37\x30\x30\x3A\x63\x66\x31\x64\x3A\x3A\x10\x9D\x9E\x03\x18\xDB\xEC\xD4\xE9\x05')

	conn.start_transaction()

	cursor = conn.cursor(prepared=True)

	sql = "insert into test_binary values(%s)"
	cursor.execute(sql, params)

	sql = "select * from test_binary"
	cursor.execute(sql)

	results = cursor.fetchall()
	for row in results:
		print(row[0])

	sql = "delete from test_binary"
	cursor.execute(sql)

	conn.commit()

except Exception as err:
	logging.error(err)

finally:
	if conn:
		conn.disconnect()

Suggested fix:
this bug is imported since mysql-connector-python 8.0.11, it is ok for connector 8.0.6.

in _parse_binary_values(), https://github.com/mysql/mysql-connector-python/blob/master/lib/mysql/connector/protocol.p..., it try decode a bytearry, which may fail.
[24 Jul 2019 12:15] MySQL Verification Team
Hello zy t!

Thank you for the report.

regards,
Umesh
[12 Nov 2019 20:01] OCA Admin
Contribution submitted via Github - Fix BUG96280 
(*) Contribution by Naoki Someya (Github n-someya, mysql-connector-python/pull/55#issuecomment-552143467): 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_338568511.txt (text/plain), 4.36 KiB.

[21 Oct 2022 21:14] Philip Olson
Posted by developer:
 
Fixed as of the upcoming MySQL Connector/Python 8.0.32 release, and here's the proposed changelog entry from the documentation team:

When fetching results from a prepared cursor using the pure Python
implementation, it would fail if the VARBINARY column contained bytes that
could not be decoded. The bytes are now returned if they cannot be
decoded. 

 Thanks to Naoki Someya for the contribution.

Thank you for the bug report.