Skip to content

Commit d5ba731

Browse files
committed
BUG#28491115: Connector/Python crashes on 0 time value
The Connector/Python binary protocol implemented in MySQLProtocol._parse_binary_time() crashes on time value of 0 because it does not deal with payload of size 0 correctly. Expected behavior is to receive a 00:00:00 time when using a 0-time value payload. With this patch, the issue is fixed by addressing the edge case in which packet size is 0. This patch also fixes BUG#34006512 (Connector/Python Throws Exception When Time Field Is 0). Change-Id: If5efa22812215fb90c86589aed5ca1084b79723f
1 parent 73ce564 commit d5ba731

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

CHANGES.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ Copyright (c) 2009, 2022, Oracle and/or its affiliates.
88
Full release notes:
99
http://dev.mysql.com/doc/relnotes/connector-python/en/
1010

11+
v8.0.31
12+
=======
13+
14+
- BUG#28491115: Connector/Python crashes on 0 time value
15+
1116
v8.0.30
1217
=======
1318

lib/mysql/connector/protocol.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,8 @@ def _parse_binary_timestamp(packet):
539539
def _parse_binary_time(packet):
540540
"""Parse a time value from a binary packet"""
541541
length = packet[0]
542+
if not length:
543+
return (packet[1:], datetime.timedelta())
542544
data = packet[1 : length + 1]
543545
mcs = 0
544546
if length > 8:

tests/test_bugs.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import os
4747
import pickle
4848
import platform
49+
import struct
4950
import sys
5051
import tempfile
5152
import time
@@ -6940,3 +6941,41 @@ def test_no_backslash_escapes(self):
69406941
self.assertEqual(res[0][0], "test")
69416942
self.assertEqual(self.cnx.sql_mode, "NO_BACKSLASH_ESCAPES")
69426943
cur.execute(f"DROP TABLE IF EXISTS {table}")
6944+
6945+
6946+
class BugOra28491115(tests.MySQLConnectorTests):
6947+
"""BUG#28491115: MySQL-Connector-Python Crashes On 0 Time Value.
6948+
6949+
a) The mysql-python-connector binary protocol appears to crash on
6950+
time value of 0 because it does not deal with payload of size 0
6951+
correctly.
6952+
b) When querying a TIME field that is set to 00:00:00,
6953+
mysql.connector throws an exception when trying to unpack the
6954+
result.
6955+
6956+
Also fixes BUG#34006512 (Mysql.Connector Throws Exception When Time Field Is 0).
6957+
"""
6958+
6959+
@foreach_cnx()
6960+
def test_crash_on_time_0value(self):
6961+
table_name = "BugOra28491115"
6962+
with self.cnx.cursor(prepared=True) as cur:
6963+
cur.execute(f"DROP TABLE IF EXISTS {table_name}")
6964+
cur.execute(f"CREATE TABLE {table_name} (a TIME)")
6965+
cur.execute(f"INSERT INTO {table_name} VALUES (0)")
6966+
cur.execute(f"SELECT * FROM {table_name}")
6967+
res = cur.fetchall()
6968+
self.assertEqual(res[0][0], timedelta(0))
6969+
cur.execute(f"DROP TABLE IF EXISTS {table_name}")
6970+
6971+
@foreach_cnx()
6972+
def test_exception_when_time_field_is_0(self):
6973+
table_name = "BugOra34006512"
6974+
with self.cnx.cursor(prepared=True) as cur:
6975+
cur.execute(f"DROP TABLE IF EXISTS {table_name}")
6976+
cur.execute(f"CREATE TEMPORARY TABLE {table_name} (b TIME)")
6977+
cur.execute(f"INSERT INTO {table_name} SET b='00:00'")
6978+
cur.execute(f"SELECT * FROM {table_name}")
6979+
res = cur.fetchall()
6980+
self.assertEqual(res[0][0], timedelta(0))
6981+
cur.execute(f"DROP TABLE IF EXISTS {table_name}")

tests/test_protocol.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ def test__parse_binary_time(self):
431431
datetime.timedelta(10, 58530, 230000),
432432
bytearray(b"\x0c\x00\x0a\x00\x00\x00" b"\x10\x0f\x1e\x70\x82\x03\x00"),
433433
),
434+
(
435+
datetime.timedelta(0),
436+
bytearray(b"\x00"),
437+
),
434438
]
435439
for exp, data in cases:
436440
res = self._protocol._parse_binary_time(data + b"\x00\x00")

0 commit comments

Comments
 (0)