Skip to content

Commit

Permalink
Use defer to close rows to avoid scan panic leak rows
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Mar 29, 2022
1 parent 9dd6ed9 commit ea8509b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion callbacks/create.go
Expand Up @@ -84,8 +84,10 @@ func Create(config *Config) func(db *gorm.DB) {
db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...,
)
if db.AddError(err) == nil {
defer func() {
db.AddError(rows.Close())
}()
gorm.Scan(rows, db, mode)
db.AddError(rows.Close())
}

return
Expand Down
4 changes: 3 additions & 1 deletion callbacks/query.go
Expand Up @@ -20,8 +20,10 @@ func Query(db *gorm.DB) {
db.AddError(err)
return
}
defer func() {
db.AddError(rows.Close())
}()
gorm.Scan(rows, db, 0)
db.AddError(rows.Close())
}
}
}
Expand Down

0 comments on commit ea8509b

Please sign in to comment.