mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
database/sql: ensure releaseConn is defined before a possible close
When running a Query on Stmt a dependency is added to the stmt and rows. To do that it needs a reference to Rows, so the releaseConn function is defined after the definition. However the rows.initContextClose was set to run before the releaseConn was set on rows, setting up a situation where the connection could be canceled before the releaseConn was set and resulting in a segfault. Fixes #20160 Change-Id: I5592e7db2cf653dfc48d42cbc2b03ca20501b1a0 Reviewed-on: https://go-review.googlesource.com/42139 Run-TryBot: Daniel Theophanes <kardianos@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
295d160e01
commit
2133d63fa8
2 changed files with 46 additions and 1 deletions
|
|
@ -2183,12 +2183,17 @@ func (s *Stmt) QueryContext(ctx context.Context, args ...interface{}) (*Rows, er
|
|||
rowsi: rowsi,
|
||||
// releaseConn set below
|
||||
}
|
||||
rows.initContextClose(ctx)
|
||||
// addDep must be added before initContextClose or it could attempt
|
||||
// to removeDep before it has been added.
|
||||
s.db.addDep(s, rows)
|
||||
|
||||
// releaseConn must be set before initContextClose or it could
|
||||
// release the connection before it is set.
|
||||
rows.releaseConn = func(err error) {
|
||||
releaseConn(err)
|
||||
s.db.removeDep(s, rows)
|
||||
}
|
||||
rows.initContextClose(ctx)
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue