@@ -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 {
@@ -286,10 +286,10 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, plugin string
286
286
}
287
287
288
288
// Calculate packet length and get buffer with that size
289
- data := mc .buf .takeSmallBuffer (pktLen + 4 )
290
- if data = = nil {
289
+ data , err := mc .buf .takeSmallBuffer (pktLen + 4 )
290
+ if err ! = nil {
291
291
// cannot take the buffer. Something must be wrong with the connection
292
- errLog .Print (ErrBusyBuffer )
292
+ errLog .Print (err )
293
293
return errBadConnNoWrite
294
294
}
295
295
@@ -367,10 +367,10 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, plugin string
367
367
// http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchResponse
368
368
func (mc * mysqlConn ) writeAuthSwitchPacket (authData []byte ) error {
369
369
pktLen := 4 + len (authData )
370
- data := mc .buf .takeSmallBuffer (pktLen )
371
- if data = = nil {
370
+ data , err := mc .buf .takeSmallBuffer (pktLen )
371
+ if err ! = nil {
372
372
// cannot take the buffer. Something must be wrong with the connection
373
- errLog .Print (ErrBusyBuffer )
373
+ errLog .Print (err )
374
374
return errBadConnNoWrite
375
375
}
376
376
@@ -387,10 +387,10 @@ func (mc *mysqlConn) writeCommandPacket(command byte) error {
387
387
// Reset Packet Sequence
388
388
mc .sequence = 0
389
389
390
- data := mc .buf .takeSmallBuffer (4 + 1 )
391
- if data = = nil {
390
+ data , err := mc .buf .takeSmallBuffer (4 + 1 )
391
+ if err ! = nil {
392
392
// cannot take the buffer. Something must be wrong with the connection
393
- errLog .Print (ErrBusyBuffer )
393
+ errLog .Print (err )
394
394
return errBadConnNoWrite
395
395
}
396
396
@@ -406,10 +406,10 @@ func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error {
406
406
mc .sequence = 0
407
407
408
408
pktLen := 1 + len (arg )
409
- data := mc .buf .takeBuffer (pktLen + 4 )
410
- if data = = nil {
409
+ data , err := mc .buf .takeBuffer (pktLen + 4 )
410
+ if err ! = nil {
411
411
// cannot take the buffer. Something must be wrong with the connection
412
- errLog .Print (ErrBusyBuffer )
412
+ errLog .Print (err )
413
413
return errBadConnNoWrite
414
414
}
415
415
@@ -427,10 +427,10 @@ func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error {
427
427
// Reset Packet Sequence
428
428
mc .sequence = 0
429
429
430
- data := mc .buf .takeSmallBuffer (4 + 1 + 4 )
431
- if data = = nil {
430
+ data , err := mc .buf .takeSmallBuffer (4 + 1 + 4 )
431
+ if err ! = nil {
432
432
// cannot take the buffer. Something must be wrong with the connection
433
- errLog .Print (ErrBusyBuffer )
433
+ errLog .Print (err )
434
434
return errBadConnNoWrite
435
435
}
436
436
@@ -883,7 +883,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
883
883
const minPktLen = 4 + 1 + 4 + 1 + 4
884
884
mc := stmt .mc
885
885
886
- // Determine threshould dynamically to avoid packet size shortage.
886
+ // Determine threshold dynamically to avoid packet size shortage.
887
887
longDataSize := mc .maxAllowedPacket / (stmt .paramCount + 1 )
888
888
if longDataSize < 64 {
889
889
longDataSize = 64
@@ -893,15 +893,17 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
893
893
mc .sequence = 0
894
894
895
895
var data []byte
896
+ var err error
896
897
897
898
if len (args ) == 0 {
898
- data = mc .buf .takeBuffer (minPktLen )
899
+ data , err = mc .buf .takeBuffer (minPktLen )
899
900
} else {
900
- data = mc .buf .takeCompleteBuffer ()
901
+ data , err = mc .buf .takeCompleteBuffer ()
902
+ // In this case the len(data) == cap(data) which is used to optimise the flow below.
901
903
}
902
- if data = = nil {
904
+ if err ! = nil {
903
905
// cannot take the buffer. Something must be wrong with the connection
904
- errLog .Print (ErrBusyBuffer )
906
+ errLog .Print (err )
905
907
return errBadConnNoWrite
906
908
}
907
909
@@ -927,7 +929,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
927
929
pos := minPktLen
928
930
929
931
var nullMask []byte
930
- if maskLen , typesLen := (len (args )+ 7 )/ 8 , 1 + 2 * len (args ); pos + maskLen + typesLen >= len (data ) {
932
+ if maskLen , typesLen := (len (args )+ 7 )/ 8 , 1 + 2 * len (args ); pos + maskLen + typesLen >= cap (data ) {
931
933
// buffer has to be extended but we don't know by how much so
932
934
// we depend on append after all data with known sizes fit.
933
935
// We stop at that because we deal with a lot of columns here
@@ -936,10 +938,11 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
936
938
copy (tmp [:pos ], data [:pos ])
937
939
data = tmp
938
940
nullMask = data [pos : pos + maskLen ]
941
+ // No need to clean nullMask as make ensures that.
939
942
pos += maskLen
940
943
} else {
941
944
nullMask = data [pos : pos + maskLen ]
942
- for i := 0 ; i < maskLen ; i ++ {
945
+ for i := range nullMask {
943
946
nullMask [i ] = 0
944
947
}
945
948
pos += maskLen
@@ -1076,7 +1079,10 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
1076
1079
// In that case we must build the data packet with the new values buffer
1077
1080
if valuesCap != cap (paramValues ) {
1078
1081
data = append (data [:pos ], paramValues ... )
1079
- mc .buf .buf = data
1082
+ if err = mc .buf .store (data ); err != nil {
1083
+ errLog .Print (err )
1084
+ return errBadConnNoWrite
1085
+ }
1080
1086
}
1081
1087
1082
1088
pos += len (paramValues )
0 commit comments