@@ -51,7 +51,7 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
5151 mc .sequence ++
5252
5353 // 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
5555 if pktLen == 0 {
5656 // there was no previous packet
5757 if prevData == nil {
@@ -286,10 +286,10 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, plugin string
286286 }
287287
288288 // 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 {
291291 // cannot take the buffer. Something must be wrong with the connection
292- errLog .Print (ErrBusyBuffer )
292+ errLog .Print (err )
293293 return errBadConnNoWrite
294294 }
295295
@@ -367,10 +367,10 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, plugin string
367367// http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchResponse
368368func (mc * mysqlConn ) writeAuthSwitchPacket (authData []byte ) error {
369369 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 {
372372 // cannot take the buffer. Something must be wrong with the connection
373- errLog .Print (ErrBusyBuffer )
373+ errLog .Print (err )
374374 return errBadConnNoWrite
375375 }
376376
@@ -387,10 +387,10 @@ func (mc *mysqlConn) writeCommandPacket(command byte) error {
387387 // Reset Packet Sequence
388388 mc .sequence = 0
389389
390- data := mc .buf .takeSmallBuffer (4 + 1 )
391- if data = = nil {
390+ data , err := mc .buf .takeSmallBuffer (4 + 1 )
391+ if err ! = nil {
392392 // cannot take the buffer. Something must be wrong with the connection
393- errLog .Print (ErrBusyBuffer )
393+ errLog .Print (err )
394394 return errBadConnNoWrite
395395 }
396396
@@ -406,10 +406,10 @@ func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error {
406406 mc .sequence = 0
407407
408408 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 {
411411 // cannot take the buffer. Something must be wrong with the connection
412- errLog .Print (ErrBusyBuffer )
412+ errLog .Print (err )
413413 return errBadConnNoWrite
414414 }
415415
@@ -427,10 +427,10 @@ func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error {
427427 // Reset Packet Sequence
428428 mc .sequence = 0
429429
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 {
432432 // cannot take the buffer. Something must be wrong with the connection
433- errLog .Print (ErrBusyBuffer )
433+ errLog .Print (err )
434434 return errBadConnNoWrite
435435 }
436436
@@ -883,7 +883,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
883883 const minPktLen = 4 + 1 + 4 + 1 + 4
884884 mc := stmt .mc
885885
886- // Determine threshould dynamically to avoid packet size shortage.
886+ // Determine threshold dynamically to avoid packet size shortage.
887887 longDataSize := mc .maxAllowedPacket / (stmt .paramCount + 1 )
888888 if longDataSize < 64 {
889889 longDataSize = 64
@@ -893,15 +893,17 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
893893 mc .sequence = 0
894894
895895 var data []byte
896+ var err error
896897
897898 if len (args ) == 0 {
898- data = mc .buf .takeBuffer (minPktLen )
899+ data , err = mc .buf .takeBuffer (minPktLen )
899900 } 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.
901903 }
902- if data = = nil {
904+ if err ! = nil {
903905 // cannot take the buffer. Something must be wrong with the connection
904- errLog .Print (ErrBusyBuffer )
906+ errLog .Print (err )
905907 return errBadConnNoWrite
906908 }
907909
@@ -927,7 +929,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
927929 pos := minPktLen
928930
929931 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 ) {
931933 // buffer has to be extended but we don't know by how much so
932934 // we depend on append after all data with known sizes fit.
933935 // 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 {
936938 copy (tmp [:pos ], data [:pos ])
937939 data = tmp
938940 nullMask = data [pos : pos + maskLen ]
941+ // No need to clean nullMask as make ensures that.
939942 pos += maskLen
940943 } else {
941944 nullMask = data [pos : pos + maskLen ]
942- for i := 0 ; i < maskLen ; i ++ {
945+ for i := range nullMask {
943946 nullMask [i ] = 0
944947 }
945948 pos += maskLen
@@ -1076,7 +1079,10 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
10761079 // In that case we must build the data packet with the new values buffer
10771080 if valuesCap != cap (paramValues ) {
10781081 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+ }
10801086 }
10811087
10821088 pos += len (paramValues )
0 commit comments