diff options
author | Kiril Vladimiroff <kiril@vladimiroff.org> | 2020-02-11 16:12:21 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-11 14:12:21 +0000 |
commit | d5dbe546e461261056b5fda1a2ac9fc6d36c69e1 (patch) | |
tree | fc5f950d5c4e51fe2167239b847c4e6d9b01831b /clientapi | |
parent | d45f869cdd35b07ed9b44445732b27935ca1910d (diff) |
Always defer *sql.Rows.Close and consult with Err (#844)
* Always defer *sql.Rows.Close and consult with Err
database/sql.Rows.Next() makes sure to call Close only after exhausting
result rows which would NOT happen when returning early from a bad Scan.
Close being idempotent makes it a great candidate to get always deferred
regardless of what happens later on the result set.
This change also makes sure call Err() after exhausting Next() and
propagate non-nil results from it as the documentation advises.
Closes #764
Signed-off-by: Kiril Vladimiroff <kiril@vladimiroff.org>
* Override named result parameters in last returns
Signed-off-by: Kiril Vladimiroff <kiril@vladimiroff.org>
* Do the same over new changes that got merged
Signed-off-by: Kiril Vladimiroff <kiril@vladimiroff.org>
Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com>
Diffstat (limited to 'clientapi')
4 files changed, 9 insertions, 9 deletions
diff --git a/clientapi/auth/storage/accounts/account_data_table.go b/clientapi/auth/storage/accounts/account_data_table.go index 080ca3f3..1b7484d8 100644 --- a/clientapi/auth/storage/accounts/account_data_table.go +++ b/clientapi/auth/storage/accounts/account_data_table.go @@ -90,6 +90,7 @@ func (s *accountDataStatements) selectAccountData( if err != nil { return } + defer rows.Close() // nolint: errcheck global = []gomatrixserverlib.ClientEvent{} rooms = make(map[string][]gomatrixserverlib.ClientEvent) @@ -114,8 +115,7 @@ func (s *accountDataStatements) selectAccountData( global = append(global, ac) } } - - return + return global, rooms, rows.Err() } func (s *accountDataStatements) selectAccountDataByType( diff --git a/clientapi/auth/storage/accounts/membership_table.go b/clientapi/auth/storage/accounts/membership_table.go index 6185065c..7b7c50ac 100644 --- a/clientapi/auth/storage/accounts/membership_table.go +++ b/clientapi/auth/storage/accounts/membership_table.go @@ -122,11 +122,10 @@ func (s *membershipStatements) selectMembershipsByLocalpart( for rows.Next() { var m authtypes.Membership m.Localpart = localpart - if err := rows.Scan(&m.RoomID, &m.EventID); err != nil { - return nil, err + if err = rows.Scan(&m.RoomID, &m.EventID); err != nil { + return } memberships = append(memberships, m) } - - return + return memberships, rows.Err() } diff --git a/clientapi/auth/storage/accounts/threepid_table.go b/clientapi/auth/storage/accounts/threepid_table.go index 5900260a..a03aa4f8 100644 --- a/clientapi/auth/storage/accounts/threepid_table.go +++ b/clientapi/auth/storage/accounts/threepid_table.go @@ -97,6 +97,7 @@ func (s *threepidStatements) selectThreePIDsForLocalpart( if err != nil { return } + defer rows.Close() // nolint: errcheck threepids = []authtypes.ThreePID{} for rows.Next() { @@ -110,8 +111,7 @@ func (s *threepidStatements) selectThreePIDsForLocalpart( Medium: medium, }) } - - return + return threepids, rows.Err() } func (s *threepidStatements) insertThreePID( diff --git a/clientapi/auth/storage/devices/devices_table.go b/clientapi/auth/storage/devices/devices_table.go index c5773ce3..99741247 100644 --- a/clientapi/auth/storage/devices/devices_table.go +++ b/clientapi/auth/storage/devices/devices_table.go @@ -226,6 +226,7 @@ func (s *devicesStatements) selectDevicesByLocalpart( if err != nil { return devices, err } + defer rows.Close() // nolint: errcheck for rows.Next() { var dev authtypes.Device @@ -237,5 +238,5 @@ func (s *devicesStatements) selectDevicesByLocalpart( devices = append(devices, dev) } - return devices, nil + return devices, rows.Err() } |