Skip to content

Commit 792de22

Browse files
aarongablejsha
andauthored
Check error return of rows.Close(). (#6)
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. Note: This is a recreation of go-gorp#420 Co-authored-by: Jacob Hoffman-Andrews <github@hoffman-andrews.com>
1 parent a4dac79 commit 792de22

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

select.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,12 @@ func selectVal(e SqlExecutor, holder interface{}, query string, args ...interfac
168168
return sql.ErrNoRows
169169
}
170170

171-
return rows.Scan(holder)
171+
err = rows.Scan(holder)
172+
if err != nil {
173+
return err
174+
}
175+
176+
return rows.Close()
172177
}
173178

174179
func hookedselect(m *DbMap, exec SqlExecutor, i interface{}, query string,
@@ -351,6 +356,11 @@ func rawselect(m *DbMap, exec SqlExecutor, i interface{}, query string,
351356
}
352357
}
353358

359+
err = rows.Close()
360+
if err != nil {
361+
return nil, err
362+
}
363+
354364
if appendToSlice && sliceValue.IsNil() {
355365
sliceValue.Set(reflect.MakeSlice(sliceValue.Type(), 0, 0))
356366
}

0 commit comments

Comments
 (0)