fix: close resultsets so we don't leak them (#1809)

* fix: close resultsets so we don't leak them

* Refactor browsers/database

To implement PR suggestions and improve code quality.

* Refactor services/permissions/database

To implement PR suggestions and improve code quality.

Co-authored-by: Samuel Hawksby-Robinson <samuel@samyoul.com>
This commit is contained in:
André Medeiros 2020-05-14 06:51:32 -04:00 committed by GitHub
parent 77d9d0f4c9
commit be683556ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 53 additions and 44 deletions

View File

@ -353,6 +353,7 @@ func (db *Database) GetAccounts() ([]Account, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rows.Close()
accounts := []Account{} accounts := []Account{}
pubkey := []byte{} pubkey := []byte{}
for rows.Next() { for rows.Next() {
@ -456,6 +457,7 @@ func (db *Database) GetAddresses() (rst []types.Address, err error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rows.Close()
for rows.Next() { for rows.Next() {
addr := types.Address{} addr := types.Address{}
err = rows.Scan(&addr) err = rows.Scan(&addr)

View File

@ -42,6 +42,7 @@ func (db *Database) GetAccounts() ([]Account, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rows.Close()
rst := []Account{} rst := []Account{}
inthelper := sql.NullInt64{} inthelper := sql.NullInt64{}
for rows.Next() { for rows.Next() {

View File

@ -26,6 +26,7 @@ func (s *sqlitePersistence) GetActiveInstallations(maxInstallations int, identit
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rows.Close()
for rows.Next() { for rows.Next() {
var ( var (

View File

@ -77,6 +77,7 @@ func (s *sqlitePersistence) Get(identity []byte, installationIDs []string) (*Res
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {
return nil, err return nil, err
} }
defer rows.Close()
for rows.Next() { for rows.Next() {
var installationID string var installationID string
@ -102,6 +103,7 @@ func (s *sqlitePersistence) All() ([][][]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rows.Close()
for rows.Next() { for rows.Next() {
var secret []byte var secret []byte

View File

@ -39,6 +39,7 @@ func (s *sqlitePersistence) All() (map[string][]byte, error) {
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {
return nil, err return nil, err
} }
defer rows.Close()
for rows.Next() { for rows.Next() {
var ( var (

View File

@ -39,6 +39,7 @@ func (s *sqlitePersistence) All() (map[string][]byte, error) {
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {
return nil, err return nil, err
} }
defer rows.Close()
for rows.Next() { for rows.Next() {
var ( var (

View File

@ -28,11 +28,7 @@ type Browser struct {
} }
func (db *Database) InsertBrowser(browser Browser) (err error) { func (db *Database) InsertBrowser(browser Browser) (err error) {
var ( tx, err := db.db.Begin()
tx *sql.Tx
insert *sql.Stmt
)
tx, err = db.db.Begin()
if err != nil { if err != nil {
return return
} }
@ -43,38 +39,37 @@ func (db *Database) InsertBrowser(browser Browser) (err error) {
} }
_ = tx.Rollback() _ = tx.Rollback()
}() }()
insert, err = tx.Prepare("INSERT OR REPLACE INTO browsers(id, name, timestamp, dapp, historyIndex) VALUES(?, ?, ?, ?, ?)")
bInsert, err := tx.Prepare("INSERT OR REPLACE INTO browsers(id, name, timestamp, dapp, historyIndex) VALUES(?, ?, ?, ?, ?)")
if err != nil { if err != nil {
return return
} }
_, err = insert.Exec(browser.ID, browser.Name, browser.Timestamp, browser.Dapp, browser.HistoryIndex) _, err = bInsert.Exec(browser.ID, browser.Name, browser.Timestamp, browser.Dapp, browser.HistoryIndex)
insert.Close() bInsert.Close()
if err != nil { if err != nil {
return return
} }
if len(browser.History) == 0 { if len(browser.History) == 0 {
return return
} }
insert, err = tx.Prepare("INSERT INTO browsers_history(browser_id, history) VALUES(?, ?)") bhInsert, err := tx.Prepare("INSERT INTO browsers_history(browser_id, history) VALUES(?, ?)")
if err != nil { if err != nil {
return return
} }
defer insert.Close() defer bhInsert.Close()
for _, history := range browser.History { for _, history := range browser.History {
_, err = insert.Exec(browser.ID, history) _, err = bhInsert.Exec(browser.ID, history)
if err != nil { if err != nil {
return return
} }
} }
return return
} }
func (db *Database) GetBrowsers() (rst []*Browser, err error) { func (db *Database) GetBrowsers() (rst []*Browser, err error) {
var ( tx, err := db.db.Begin()
tx *sql.Tx
rows *sql.Rows
)
tx, err = db.db.Begin()
if err != nil { if err != nil {
return return
} }
@ -85,36 +80,41 @@ func (db *Database) GetBrowsers() (rst []*Browser, err error) {
} }
_ = tx.Rollback() _ = tx.Rollback()
}() }()
// FULL and RIGHT joins are not supported // FULL and RIGHT joins are not supported
rows, err = tx.Query("SELECT id, name, timestamp, dapp, historyIndex FROM browsers ORDER BY timestamp DESC") bRows, err := tx.Query("SELECT id, name, timestamp, dapp, historyIndex FROM browsers ORDER BY timestamp DESC")
if err != nil { if err != nil {
return return
} }
defer bRows.Close()
browsers := map[string]*Browser{} browsers := map[string]*Browser{}
for rows.Next() { for bRows.Next() {
browser := Browser{} browser := Browser{}
err = rows.Scan(&browser.ID, &browser.Name, &browser.Timestamp, &browser.Dapp, &browser.HistoryIndex) err = bRows.Scan(&browser.ID, &browser.Name, &browser.Timestamp, &browser.Dapp, &browser.HistoryIndex)
if err != nil { if err != nil {
return nil, err return nil, err
} }
browsers[browser.ID] = &browser browsers[browser.ID] = &browser
rst = append(rst, &browser) rst = append(rst, &browser)
} }
rows, err = tx.Query("SELECT browser_id, history from browsers_history")
bhRows, err := tx.Query("SELECT browser_id, history from browsers_history")
if err != nil { if err != nil {
return return
} }
defer bhRows.Close()
var ( var (
id string id string
history string history string
) )
for rows.Next() { for bhRows.Next() {
err = rows.Scan(&id, &history) err = bhRows.Scan(&id, &history)
if err != nil { if err != nil {
return return
} }
browsers[id].History = append(browsers[id].History, history) browsers[id].History = append(browsers[id].History, history)
} }
return rst, nil return rst, nil
} }

View File

@ -234,6 +234,7 @@ func (d *Database) Topics() ([]MailserverTopic, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rows.Close()
for rows.Next() { for rows.Next() {
var ( var (
@ -281,6 +282,7 @@ func (d *Database) ChatRequestRanges() ([]ChatRequestRange, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rows.Close()
for rows.Next() { for rows.Next() {
var req ChatRequestRange var req ChatRequestRange

View File

@ -24,11 +24,7 @@ type DappPermissions struct {
} }
func (db *Database) AddPermissions(perms DappPermissions) (err error) { func (db *Database) AddPermissions(perms DappPermissions) (err error) {
var ( tx, err := db.db.Begin()
tx *sql.Tx
insert *sql.Stmt
)
tx, err = db.db.Begin()
if err != nil { if err != nil {
return return
} }
@ -39,25 +35,27 @@ func (db *Database) AddPermissions(perms DappPermissions) (err error) {
} }
_ = tx.Rollback() _ = tx.Rollback()
}() }()
insert, err = tx.Prepare("INSERT OR REPLACE INTO dapps(name) VALUES(?)")
dInsert, err := tx.Prepare("INSERT OR REPLACE INTO dapps(name) VALUES(?)")
if err != nil { if err != nil {
return return
} }
_, err = insert.Exec(perms.Name) _, err = dInsert.Exec(perms.Name)
insert.Close() dInsert.Close()
if err != nil { if err != nil {
return return
} }
if len(perms.Permissions) == 0 { if len(perms.Permissions) == 0 {
return return
} }
insert, err = tx.Prepare("INSERT INTO permissions(dapp_name, permission) VALUES(?, ?)") pInsert, err := tx.Prepare("INSERT INTO permissions(dapp_name, permission) VALUES(?, ?)")
if err != nil { if err != nil {
return return
} }
defer insert.Close() defer pInsert.Close()
for _, perm := range perms.Permissions { for _, perm := range perms.Permissions {
_, err = insert.Exec(perms.Name, perm) _, err = pInsert.Exec(perms.Name, perm)
if err != nil { if err != nil {
return return
} }
@ -66,11 +64,7 @@ func (db *Database) AddPermissions(perms DappPermissions) (err error) {
} }
func (db *Database) GetPermissions() (rst []DappPermissions, err error) { func (db *Database) GetPermissions() (rst []DappPermissions, err error) {
var ( tx, err := db.db.Begin()
tx *sql.Tx
rows *sql.Rows
)
tx, err = db.db.Begin()
if err != nil { if err != nil {
return return
} }
@ -81,30 +75,34 @@ func (db *Database) GetPermissions() (rst []DappPermissions, err error) {
} }
_ = tx.Rollback() _ = tx.Rollback()
}() }()
// FULL and RIGHT joins are not supported // FULL and RIGHT joins are not supported
rows, err = tx.Query("SELECT name FROM dapps") dRows, err := tx.Query("SELECT name FROM dapps")
if err != nil { if err != nil {
return return
} }
defer dRows.Close()
dapps := map[string]*DappPermissions{} dapps := map[string]*DappPermissions{}
for rows.Next() { for dRows.Next() {
perms := DappPermissions{} perms := DappPermissions{}
err = rows.Scan(&perms.Name) err = dRows.Scan(&perms.Name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
dapps[perms.Name] = &perms dapps[perms.Name] = &perms
} }
rows, err = tx.Query("SELECT dapp_name, permission from permissions")
pRows, err := tx.Query("SELECT dapp_name, permission from permissions")
if err != nil { if err != nil {
return return
} }
defer pRows.Close()
var ( var (
name string name string
permission string permission string
) )
for rows.Next() { for pRows.Next() {
err = rows.Scan(&name, &permission) err = pRows.Scan(&name, &permission)
if err != nil { if err != nil {
return return
} }
@ -114,6 +112,7 @@ func (db *Database) GetPermissions() (rst []DappPermissions, err error) {
for key := range dapps { for key := range dapps {
rst = append(rst, *dapps[key]) rst = append(rst, *dapps[key])
} }
return rst, nil return rst, nil
} }