@@ -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 {
@@ -288,10 +288,10 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, addNUL bool,
288288 }
289289
290290 // 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 {
293293 // cannot take the buffer. Something must be wrong with the connection
294- errLog .Print (ErrBusyBuffer )
294+ errLog .Print (err )
295295 return errBadConnNoWrite
296296 }
297297
@@ -375,10 +375,10 @@ func (mc *mysqlConn) writeAuthSwitchPacket(authData []byte, addNUL bool) error {
375375 if addNUL {
376376 pktLen ++
377377 }
378- data := mc .buf .takeSmallBuffer (pktLen )
379- if data = = nil {
378+ data , err := mc .buf .takeSmallBuffer (pktLen )
379+ if err ! = nil {
380380 // cannot take the buffer. Something must be wrong with the connection
381- errLog .Print (ErrBusyBuffer )
381+ errLog .Print (err )
382382 return errBadConnNoWrite
383383 }
384384
@@ -399,10 +399,10 @@ func (mc *mysqlConn) writeCommandPacket(command byte) error {
399399 // Reset Packet Sequence
400400 mc .sequence = 0
401401
402- data := mc .buf .takeSmallBuffer (4 + 1 )
403- if data = = nil {
402+ data , err := mc .buf .takeSmallBuffer (4 + 1 )
403+ if err ! = nil {
404404 // cannot take the buffer. Something must be wrong with the connection
405- errLog .Print (ErrBusyBuffer )
405+ errLog .Print (err )
406406 return errBadConnNoWrite
407407 }
408408
@@ -418,10 +418,10 @@ func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error {
418418 mc .sequence = 0
419419
420420 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 {
423423 // cannot take the buffer. Something must be wrong with the connection
424- errLog .Print (ErrBusyBuffer )
424+ errLog .Print (err )
425425 return errBadConnNoWrite
426426 }
427427
@@ -439,10 +439,10 @@ func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error {
439439 // Reset Packet Sequence
440440 mc .sequence = 0
441441
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 {
444444 // cannot take the buffer. Something must be wrong with the connection
445- errLog .Print (ErrBusyBuffer )
445+ errLog .Print (err )
446446 return errBadConnNoWrite
447447 }
448448
@@ -895,7 +895,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
895895 const minPktLen = 4 + 1 + 4 + 1 + 4
896896 mc := stmt .mc
897897
898- // Determine threshould dynamically to avoid packet size shortage.
898+ // Determine threshold dynamically to avoid packet size shortage.
899899 longDataSize := mc .maxAllowedPacket / (stmt .paramCount + 1 )
900900 if longDataSize < 64 {
901901 longDataSize = 64
@@ -905,15 +905,16 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
905905 mc .sequence = 0
906906
907907 var data []byte
908+ var err error
908909
909910 if len (args ) == 0 {
910- data = mc .buf .takeBuffer (minPktLen )
911+ data , err = mc .buf .takeBuffer (minPktLen )
911912 } else {
912- data = mc .buf .takeCompleteBuffer ()
913+ data , err = mc .buf .takeCompleteBuffer ()
913914 }
914- if data = = nil {
915+ if err ! = nil {
915916 // cannot take the buffer. Something must be wrong with the connection
916- errLog .Print (ErrBusyBuffer )
917+ errLog .Print (err )
917918 return errBadConnNoWrite
918919 }
919920
@@ -939,23 +940,25 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
939940 pos := minPktLen
940941
941942 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 ) {
943946 // buffer has to be extended but we don't know by how much so
944947 // we depend on append after all data with known sizes fit.
945948 // We stop at that because we deal with a lot of columns here
946949 // which makes the required allocation size hard to guess.
947950 tmp := make ([]byte , pos + maskLen + typesLen )
948951 copy (tmp [:pos ], data [:pos ])
949952 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
958960 }
961+ pos += maskLen
959962
960963 // newParameterBoundFlag 1 [1 byte]
961964 data [pos ] = 0x01
0 commit comments