@@ -51,7 +51,7 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
51
51
mc .sequence ++
52
52
53
53
// packets with length 0 terminate a previous packet which is a
54
- // multiple of (2^24)− 1 bytes long
54
+ // multiple of (2^24)- 1 bytes long
55
55
if pktLen == 0 {
56
56
// there was no previous packet
57
57
if prevData == nil {
@@ -288,10 +288,10 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, addNUL bool,
288
288
}
289
289
290
290
// Calculate packet length and get buffer with that size
291
- data := mc .buf .takeSmallBuffer (pktLen + 4 )
292
- if data = = nil {
291
+ data , err := mc .buf .takeSmallBuffer (pktLen + 4 )
292
+ if err ! = nil {
293
293
// cannot take the buffer. Something must be wrong with the connection
294
- errLog .Print (ErrBusyBuffer )
294
+ errLog .Print (err )
295
295
return errBadConnNoWrite
296
296
}
297
297
@@ -375,10 +375,10 @@ func (mc *mysqlConn) writeAuthSwitchPacket(authData []byte, addNUL bool) error {
375
375
if addNUL {
376
376
pktLen ++
377
377
}
378
- data := mc .buf .takeSmallBuffer (pktLen )
379
- if data = = nil {
378
+ data , err := mc .buf .takeSmallBuffer (pktLen )
379
+ if err ! = nil {
380
380
// cannot take the buffer. Something must be wrong with the connection
381
- errLog .Print (ErrBusyBuffer )
381
+ errLog .Print (err )
382
382
return errBadConnNoWrite
383
383
}
384
384
@@ -399,10 +399,10 @@ func (mc *mysqlConn) writeCommandPacket(command byte) error {
399
399
// Reset Packet Sequence
400
400
mc .sequence = 0
401
401
402
- data := mc .buf .takeSmallBuffer (4 + 1 )
403
- if data = = nil {
402
+ data , err := mc .buf .takeSmallBuffer (4 + 1 )
403
+ if err ! = nil {
404
404
// cannot take the buffer. Something must be wrong with the connection
405
- errLog .Print (ErrBusyBuffer )
405
+ errLog .Print (err )
406
406
return errBadConnNoWrite
407
407
}
408
408
@@ -418,10 +418,10 @@ func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error {
418
418
mc .sequence = 0
419
419
420
420
pktLen := 1 + len (arg )
421
- data := mc .buf .takeBuffer (pktLen + 4 )
422
- if data = = nil {
421
+ data , err := mc .buf .takeBuffer (pktLen + 4 )
422
+ if err ! = nil {
423
423
// cannot take the buffer. Something must be wrong with the connection
424
- errLog .Print (ErrBusyBuffer )
424
+ errLog .Print (err )
425
425
return errBadConnNoWrite
426
426
}
427
427
@@ -439,10 +439,10 @@ func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error {
439
439
// Reset Packet Sequence
440
440
mc .sequence = 0
441
441
442
- data := mc .buf .takeSmallBuffer (4 + 1 + 4 )
443
- if data = = nil {
442
+ data , err := mc .buf .takeSmallBuffer (4 + 1 + 4 )
443
+ if err ! = nil {
444
444
// cannot take the buffer. Something must be wrong with the connection
445
- errLog .Print (ErrBusyBuffer )
445
+ errLog .Print (err )
446
446
return errBadConnNoWrite
447
447
}
448
448
@@ -895,7 +895,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
895
895
const minPktLen = 4 + 1 + 4 + 1 + 4
896
896
mc := stmt .mc
897
897
898
- // Determine threshould dynamically to avoid packet size shortage.
898
+ // Determine threshold dynamically to avoid packet size shortage.
899
899
longDataSize := mc .maxAllowedPacket / (stmt .paramCount + 1 )
900
900
if longDataSize < 64 {
901
901
longDataSize = 64
@@ -905,15 +905,16 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
905
905
mc .sequence = 0
906
906
907
907
var data []byte
908
+ var err error
908
909
909
910
if len (args ) == 0 {
910
- data = mc .buf .takeBuffer (minPktLen )
911
+ data , err = mc .buf .takeBuffer (minPktLen )
911
912
} else {
912
- data = mc .buf .takeCompleteBuffer ()
913
+ data , err = mc .buf .takeCompleteBuffer ()
913
914
}
914
- if data = = nil {
915
+ if err ! = nil {
915
916
// cannot take the buffer. Something must be wrong with the connection
916
- errLog .Print (ErrBusyBuffer )
917
+ errLog .Print (err )
917
918
return errBadConnNoWrite
918
919
}
919
920
@@ -939,23 +940,25 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
939
940
pos := minPktLen
940
941
941
942
var nullMask []byte
942
- if maskLen , typesLen := (len (args )+ 7 )/ 8 , 1 + 2 * len (args ); pos + maskLen + typesLen >= len (data ) {
943
+ maskLen , typesLen := (len (args )+ 7 )/ 8 , 1 + 2 * len (args )
944
+ l := pos + maskLen + typesLen
945
+ if l > cap (data ) {
943
946
// buffer has to be extended but we don't know by how much so
944
947
// we depend on append after all data with known sizes fit.
945
948
// We stop at that because we deal with a lot of columns here
946
949
// which makes the required allocation size hard to guess.
947
950
tmp := make ([]byte , pos + maskLen + typesLen )
948
951
copy (tmp [:pos ], data [:pos ])
949
952
data = tmp
950
- nullMask = data [pos : pos + maskLen ]
951
- pos += maskLen
952
- } else {
953
- nullMask = data [pos : pos + maskLen ]
954
- for i := 0 ; i < maskLen ; i ++ {
955
- nullMask [i ] = 0
956
- }
957
- pos += maskLen
953
+ } else if l > len (data ) {
954
+ data = data [:l ]
955
+ }
956
+
957
+ nullMask = data [pos : pos + maskLen ]
958
+ for i := range nullMask {
959
+ nullMask [i ] = 0
958
960
}
961
+ pos += maskLen
959
962
960
963
// newParameterBoundFlag 1 [1 byte]
961
964
data [pos ] = 0x01
0 commit comments