From a73a1fbe3034ab1fcb7b243aa9a795bd923fc7a7 Mon Sep 17 00:00:00 2001 From: Jacob Hoffman-Andrews Date: Tue, 9 Jun 2020 20:17:47 -0700 Subject: [PATCH] Check error return of rows.Close(). According to https://github.blog/2020-05-20-three-bugs-in-the-go-mysql-driver/, when QueryContext is called with a context that is cancelled during scan, you can receive incomplete or corrupted results. As I understand it, the corruption is fixed upstream, but it's still possible to get incomplete results that will only show up in the error result from Close. It's still possible and correct to call `defer rows.Close()`, since the database/sql docs say this: https://godoc.org/database/sql#Rows.Close > Close is idempotent and does not affect the result of Err. (cherry picked from commit 9179593109cb74a0a09dbf5d9676a0569ded1175) --- select.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/select.go b/select.go index cb1d6d79..e2386c6e 100644 --- a/select.go +++ b/select.go @@ -168,7 +168,12 @@ func selectVal(e SqlExecutor, holder interface{}, query string, args ...interfac return sql.ErrNoRows } - return rows.Scan(holder) + err = rows.Scan(holder) + if err != nil { + return err + } + + return rows.Close() } func hookedselect(m *DbMap, exec SqlExecutor, i interface{}, query string, @@ -351,6 +356,11 @@ func rawselect(m *DbMap, exec SqlExecutor, i interface{}, query string, } } + err = rows.Close() + if err != nil { + return nil, err + } + if appendToSlice && sliceValue.IsNil() { sliceValue.Set(reflect.MakeSlice(sliceValue.Type(), 0, 0)) }