Ensure all code paths close *sql.Rows (#679)

* ensure all code paths close *sql.Rows
This commit is contained in:
Doug Lauder 2021-07-06 16:44:11 -04:00 committed by GitHub
parent 702b4b1061
commit 6e214ebc3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 40 additions and 10 deletions

View file

@ -14,6 +14,7 @@ import (
"github.com/mattermost/focalboard/server/services/store/sqlstore"
pluginapi "github.com/mattermost/mattermost-plugin-api"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/plugin"
)
@ -125,7 +126,7 @@ func (p *Plugin) OnActivate() error {
return fmt.Errorf("error initializing the DB: %v", err)
}
if cfg.AuthMode == server.MattermostAuthMod {
layeredStore, err2 := mattermostauthlayer.New(cfg.DBType, sqlDB, db)
layeredStore, err2 := mattermostauthlayer.New(cfg.DBType, sqlDB, db, logger)
if err2 != nil {
return fmt.Errorf("error initializing the DB: %v", err2)
}

View file

@ -198,7 +198,7 @@ func NewStore(config *config.Configuration, logger *mlog.Logger) (store.Store, e
return nil, err
}
if config.AuthMode == MattermostAuthMod {
layeredStore, err2 := mattermostauthlayer.New(config.DBType, db.(*sqlstore.SQLStore).DBHandle(), db)
layeredStore, err2 := mattermostauthlayer.New(config.DBType, db.(*sqlstore.SQLStore).DBHandle(), db, logger)
if err2 != nil {
return nil, err2
}

View file

@ -11,6 +11,7 @@ import (
sq "github.com/Masterminds/squirrel"
"github.com/mattermost/focalboard/server/model"
"github.com/mattermost/focalboard/server/services/mlog"
"github.com/mattermost/focalboard/server/services/store"
)
@ -24,14 +25,16 @@ type MattermostAuthLayer struct {
store.Store
dbType string
mmDB *sql.DB
logger *mlog.Logger
}
// New creates a new SQL implementation of the store.
func New(dbType string, db *sql.DB, store store.Store) (*MattermostAuthLayer, error) {
func New(dbType string, db *sql.DB, store store.Store, logger *mlog.Logger) (*MattermostAuthLayer, error) {
layer := &MattermostAuthLayer{
Store: store,
dbType: dbType,
mmDB: db,
logger: logger,
}
return layer, nil
@ -191,6 +194,8 @@ func (s *MattermostAuthLayer) GetWorkspace(id string) (*model.Workspace, error)
if err != nil {
return nil, err
}
defer s.CloseRows(rows)
first := true
for rows.Next() {
if first {
@ -247,6 +252,7 @@ func (s *MattermostAuthLayer) GetUsersByWorkspace(workspaceID string) ([]*model.
if err != nil {
return nil, err
}
defer s.CloseRows(rows)
users, err := s.usersFromRows(rows)
if err != nil {
@ -257,8 +263,6 @@ func (s *MattermostAuthLayer) GetUsersByWorkspace(workspaceID string) ([]*model.
}
func (s *MattermostAuthLayer) usersFromRows(rows *sql.Rows) ([]*model.User, error) {
defer rows.Close()
users := []*model.User{}
for rows.Next() {
@ -292,3 +296,9 @@ func (s *MattermostAuthLayer) usersFromRows(rows *sql.Rows) ([]*model.User, erro
return users, nil
}
func (s *MattermostAuthLayer) CloseRows(rows *sql.Rows) {
if err := rows.Close(); err != nil {
s.logger.Error("error closing MattermostAuthLayer row set", mlog.Err(err))
}
}

View file

@ -41,6 +41,7 @@ func (s *SQLStore) GetBlocksWithParentAndType(c store.Container, parentID string
return nil, err
}
defer s.CloseRows(rows)
return s.blocksFromRows(rows)
}
@ -70,6 +71,7 @@ func (s *SQLStore) GetBlocksWithParent(c store.Container, parentID string) ([]mo
return nil, err
}
defer s.CloseRows(rows)
return s.blocksFromRows(rows)
}
@ -99,6 +101,7 @@ func (s *SQLStore) GetBlocksWithRootID(c store.Container, rootID string) ([]mode
return nil, err
}
defer s.CloseRows(rows)
return s.blocksFromRows(rows)
}
@ -128,6 +131,7 @@ func (s *SQLStore) GetBlocksWithType(c store.Container, blockType string) ([]mod
return nil, err
}
defer s.CloseRows(rows)
return s.blocksFromRows(rows)
}
@ -158,6 +162,7 @@ func (s *SQLStore) GetSubTree2(c store.Container, blockID string) ([]model.Block
return nil, err
}
defer s.CloseRows(rows)
return s.blocksFromRows(rows)
}
@ -196,6 +201,7 @@ func (s *SQLStore) GetSubTree3(c store.Container, blockID string) ([]model.Block
return nil, err
}
defer s.CloseRows(rows)
return s.blocksFromRows(rows)
}
@ -224,13 +230,12 @@ func (s *SQLStore) GetAllBlocks(c store.Container) ([]model.Block, error) {
return nil, err
}
defer s.CloseRows(rows)
return s.blocksFromRows(rows)
}
func (s *SQLStore) blocksFromRows(rows *sql.Rows) ([]model.Block, error) {
defer rows.Close()
results := []model.Block{}
for rows.Next() {
@ -451,6 +456,7 @@ func (s *SQLStore) GetBlockCountsByType() (map[string]int64, error) {
return nil, err
}
defer s.CloseRows(rows)
m := make(map[string]int64)

View file

@ -7,7 +7,7 @@ func (s *SQLStore) GetSystemSettings() (map[string]string, error) {
if err != nil {
return nil, err
}
defer rows.Close()
defer s.CloseRows(rows)
results := map[string]string{}

View file

@ -64,6 +64,7 @@ func (s *SQLStore) getUsersByCondition(condition sq.Eq) ([]*model.User, error) {
log.Printf("getUsersByCondition ERROR: %v", err)
return nil, err
}
defer s.CloseRows(rows)
users, err := s.usersFromRows(rows)
if err != nil {
@ -192,8 +193,6 @@ func (s *SQLStore) GetUsersByWorkspace(workspaceID string) ([]*model.User, error
}
func (s *SQLStore) usersFromRows(rows *sql.Rows) ([]*model.User, error) {
defer rows.Close()
users := []*model.User{}
for rows.Next() {

View file

@ -0,0 +1,13 @@
package sqlstore
import (
"database/sql"
"github.com/mattermost/focalboard/server/services/mlog"
)
func (s *SQLStore) CloseRows(rows *sql.Rows) {
if err := rows.Close(); err != nil {
s.logger.Error("error closing MattermostAuthLayer row set", mlog.Err(err))
}
}

View file

@ -128,6 +128,7 @@ func (s *SQLStore) GetWorkspaceCount() (int64, error) {
s.logger.Error("ERROR GetWorkspaceCount", mlog.Err(err))
return 0, err
}
defer s.CloseRows(rows)
var count int64