Skip to content

Commit 6c06989

Browse files
peeyushguptaGeert Vanderkelen
authored and
Geert Vanderkelen
committed
BUG20407036: Fix incorrect arguments to mysld_stmt_execute error
Inserting a string of length 251 to 255 using prepared statements was raising a ProgrammingError exception: 1210 (HY000): Incorrect arguments to mysqld_stmt_execute We fix this by correctly encoding the length of the string while sending it to the server. A test case has been added for BUG#20407036. (cherry picked from commit 955cfb7)
1 parent 990d601 commit 6c06989

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

lib/mysql/connector/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# MySQL Connector/Python - MySQL driver written in Python.
2-
# Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
33

44
# MySQL Connector/Python is licensed under the terms of the GPLv2
55
# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
@@ -144,7 +144,7 @@ def lc_int(i):
144144
if i < 0 or i > 18446744073709551616:
145145
raise ValueError('Requires 0 <= i <= 2^64')
146146

147-
if i <= 255:
147+
if i < 251:
148148
return bytearray(struct.pack('<B', i))
149149
elif i <= 65535:
150150
return b'\xfc' + bytearray(struct.pack('<H', i))

tests/test_bugs.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# MySQL Connector/Python - MySQL driver written in Python.
3-
# Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
3+
# Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
44

55
# MySQL Connector/Python is licensed under the terms of the GPLv2
66
# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
@@ -3110,3 +3110,47 @@ def test_warning_with_rows(self):
31103110
exp = [(u'Warning', 1642, u'TEST WARNING')]
31113111
self.assertEqual(exp, cur.fetchwarnings())
31123112
cur.close()
3113+
3114+
3115+
class BugOra20407036(tests.MySQLConnectorTests):
3116+
"""BUG#20407036: INCORRECT ARGUMENTS TO MYSQLD_STMT_EXECUTE ERROR
3117+
"""
3118+
def setUp(self):
3119+
config = tests.get_mysql_config()
3120+
self.cnx = connection.MySQLConnection(**config)
3121+
self.cur = self.cnx.cursor()
3122+
3123+
self.tbl = 'Bug20407036'
3124+
self.cur.execute("DROP TABLE IF EXISTS {0}".format(self.tbl))
3125+
3126+
create = ("CREATE TABLE {0} ( id int(10) unsigned NOT NULL, "
3127+
"text VARCHAR(70000) CHARACTER SET utf8 NOT NULL, "
3128+
"rooms tinyint(3) unsigned NOT NULL) "
3129+
"ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 "
3130+
"COLLATE=utf8_unicode_ci".format(self.tbl))
3131+
self.cur.execute(create)
3132+
3133+
def tearDown(self):
3134+
self.cur.execute("DROP TABLE IF EXISTS {0}".format(self.tbl))
3135+
self.cur.close()
3136+
self.cnx.close()
3137+
3138+
def test_binary_charset(self):
3139+
cur = self.cnx.cursor(prepared=True)
3140+
sql = "INSERT INTO {0}(text, rooms) VALUES(%s, %s)".format(self.tbl)
3141+
cur.execute(sql, ('a'*252, 1))
3142+
cur.execute(sql, ('a'*253, 2))
3143+
cur.execute(sql, ('a'*255, 3))
3144+
cur.execute(sql, ('a'*251, 4))
3145+
cur.execute(sql, ('a'*65535, 5))
3146+
3147+
exp = [
3148+
(0, 'a'*252, 1),
3149+
(0, 'a'*253, 2),
3150+
(0, 'a'*255, 3),
3151+
(0, 'a'*251, 4),
3152+
(0, 'a'*65535, 5),
3153+
]
3154+
3155+
self.cur.execute("SELECT * FROM {0}".format(self.tbl))
3156+
self.assertEqual(exp, self.cur.fetchall())

tests/test_utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# MySQL Connector/Python - MySQL driver written in Python.
2-
# Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
33

44
# MySQL Connector/Python is licensed under the terms of the GPLv2
55
# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
@@ -125,12 +125,12 @@ def test_intstore(self):
125125

126126
def test_lc_int(self):
127127
prefix = (b'', b'\xfc', b'\xfd', b'\xfe')
128+
intstore = (1, 2, 3, 8)
128129
try:
129-
for i, j in enumerate((8, 16, 24, 64)):
130-
val = 2 ** (j - 1)
131-
lenenc = utils.lc_int(val)
130+
for i, j in enumerate((128, 251, 2**24-1, 2**64-1)):
131+
lenenc = utils.lc_int(j)
132132
exp = prefix[i] + \
133-
getattr(utils, 'int{0}store'.format(int(j/8)))(val)
133+
getattr(utils, 'int{0}store'.format(intstore[i]))(j)
134134
self.assertEqual(exp, lenenc)
135135
except ValueError as err:
136136
self.fail("length_encoded_int failed for size {0}".format(j, err))

0 commit comments

Comments
 (0)