Skip to content

Commit 40352b0

Browse files
committed
Fix prepared statement
When there are many args and maxAllowedPacket is not enough, writeExecutePacket() attempted to use STMT_LONG_DATA even for 0byte string. But writeCommandLongData() doesn't support 0byte data. So it caused to send malfold packet. This commit loosen threshold for using STMT_LONG_DATA.
1 parent 9181e3a commit 40352b0

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

packets.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,11 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
912912
const minPktLen = 4 + 1 + 4 + 1 + 4
913913
mc := stmt.mc
914914

915+
longDataSize := mc.maxAllowedPacket / stmt.paramCount
916+
if longDataSize < 16 {
917+
longDataSize = 16
918+
}
919+
915920
// Reset packet-sequence
916921
mc.sequence = 0
917922

@@ -1039,7 +1044,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
10391044
paramTypes[i+i] = byte(fieldTypeString)
10401045
paramTypes[i+i+1] = 0x00
10411046

1042-
if len(v) < mc.maxAllowedPacket-pos-len(paramValues)-(len(args)-(i+1))*64 {
1047+
if len(v) < longDataSize {
10431048
paramValues = appendLengthEncodedInteger(paramValues,
10441049
uint64(len(v)),
10451050
)
@@ -1061,7 +1066,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
10611066
paramTypes[i+i] = byte(fieldTypeString)
10621067
paramTypes[i+i+1] = 0x00
10631068

1064-
if len(v) < mc.maxAllowedPacket-pos-len(paramValues)-(len(args)-(i+1))*64 {
1069+
if len(v) < longDataSize {
10651070
paramValues = appendLengthEncodedInteger(paramValues,
10661071
uint64(len(v)),
10671072
)

0 commit comments

Comments
 (0)