Skip to content

Truncate internal buffer when rows.Close() is called #904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ func newBuffer(nc net.Conn) buffer {
}
}

// discard trims b.buf[:b.idx] to prohibit it reused.
//
// This is required by Rows.Close().
// See https://github.com/golang/go/commit/651ddbdb5056ded455f47f9c494c67b389622a47
func (b *buffer) discard() {
if len(b.buf)-b.idx >= defaultBufSize {
b.buf = b.buf[b.idx:]
b.idx = 0
return
}

bufSize := defaultBufSize
if bufSize < b.length {
bufSize = b.length
}
newBuf := make([]byte, bufSize)
copy(newBuf, b.buf[b.idx:b.idx+b.length])
b.buf = newBuf
b.idx = 0
}

// fill reads into the buffer until at least _need_ bytes are in it
func (b *buffer) fill(need int) error {
n := b.length
Expand Down
4 changes: 4 additions & 0 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func (rows *mysqlRows) Close() (err error) {
return err
}

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

// Remove unread packets from stream
if !rows.rs.done {
err = mc.readUntilEOF()
Expand Down