Skip to content

Commit 30ff579

Browse files
committed
fix
1 parent b0940b6 commit 30ff579

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

buffer.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,25 @@ func newBuffer(nc net.Conn) buffer {
3737
}
3838
}
3939

40-
func (b *buffer) reset() {
41-
b.buf = make([]byte, defaultBufSize)
40+
// discard trims b.buf[:b.idx] to prohibit it reused.
41+
//
42+
// This is required by Rows.Close().
43+
// See https://github.com/golang/go/commit/651ddbdb5056ded455f47f9c494c67b389622a47
44+
func (b *buffer) discard() {
45+
if len(b.buf)-b.idx >= defaultBufSize {
46+
b.buf = b.buf[b.idx:]
47+
b.idx = 0
48+
return
49+
}
50+
51+
bufSize := defaultBufSize
52+
if bufSize < b.length {
53+
bufSize = b.length
54+
}
55+
newBuf := make([]byte, bufSize)
56+
copy(newBuf, b.buf[b.idx:b.idx+b.length])
57+
b.buf = newBuf
4258
b.idx = 0
43-
b.length = 0
4459
}
4560

4661
// fill reads into the buffer until at least _need_ bytes are in it

rows.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (rows *mysqlRows) Close() (err error) {
113113

114114
// We can't reuse receive buffer when rows.Close() is called.
115115
// See https://github.com/golang/go/commit/651ddbdb5056ded455f47f9c494c67b389622a47
116-
mc.buf.reset()
116+
mc.buf.discard()
117117

118118
// Remove unread packets from stream
119119
if !rows.rs.done {

0 commit comments

Comments
 (0)