Skip to content

Commit

Permalink
better max_allowed_packet parsing (#1661)
Browse files Browse the repository at this point in the history
Remove `stringToInt()` and use `strconv.Atoi` instead.
  • Loading branch information
methane authored Jan 22, 2025
1 parent 7403860 commit 255d1ad
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
8 changes: 7 additions & 1 deletion connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package mysql
import (
"context"
"database/sql/driver"
"fmt"
"net"
"os"
"strconv"
Expand Down Expand Up @@ -179,7 +180,12 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
mc.Close()
return nil, err
}
mc.maxAllowedPacket = stringToInt(maxap) - 1
n, err := strconv.Atoi(string(maxap))
if err != nil {
mc.Close()
return nil, fmt.Errorf("invalid max_allowed_packet value (%q): %w", maxap, err)
}
mc.maxAllowedPacket = n - 1
}
if mc.maxAllowedPacket < maxPacketSize {
mc.maxWriteSize = mc.maxAllowedPacket
Expand Down
10 changes: 0 additions & 10 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,16 +524,6 @@ func uint64ToString(n uint64) []byte {
return a[i:]
}

// treats string value as unsigned integer representation
func stringToInt(b []byte) int {
val := 0
for i := range b {
val *= 10
val += int(b[i] - 0x30)
}
return val
}

// returns the string read as a bytes slice, whether the value is NULL,
// the number of bytes read and an error, in case the string is longer than
// the input slice
Expand Down

0 comments on commit 255d1ad

Please sign in to comment.