From 255d1ad98f1d3be99661d2a8c0a7a91418acbc8d Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Wed, 22 Jan 2025 14:59:24 +0900 Subject: [PATCH] better max_allowed_packet parsing (#1661) Remove `stringToInt()` and use `strconv.Atoi` instead. --- connector.go | 8 +++++++- utils.go | 10 ---------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/connector.go b/connector.go index a4f3655e..bc1d46af 100644 --- a/connector.go +++ b/connector.go @@ -11,6 +11,7 @@ package mysql import ( "context" "database/sql/driver" + "fmt" "net" "os" "strconv" @@ -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 diff --git a/utils.go b/utils.go index 44f43ef7..8716c26c 100644 --- a/utils.go +++ b/utils.go @@ -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