Skip to content

Commit 10544ad

Browse files
committed
Merge branch 'main' of github.com:ryanrolds/sqlclosecheck into main
2 parents bb94246 + c2dc47e commit 10544ad

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

Makefile

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1+
BIN := bin
2+
13
PHONY: build install test
24

3-
build:
4-
go build
5+
$(BIN):
6+
mkdir -p $@
7+
8+
build: $(BIN)
9+
go build -o $(BIN)/sqlclosecheck .
510

611
install:
712
go install
813

9-
test: build install
14+
test: build
1015
go test ./...
1116
# Due to an issue with importing in a anaylsistest's test data some hoop jumping is required
1217
# I call twice to avoid collecting package downloads in output
13-
-go vet -vettool=${GOPATH}/bin/sqlclosecheck ./testdata/sqlx_examples
14-
-go vet -vettool=${GOPATH}/bin/sqlclosecheck ./testdata/sqlx_examples 2> sqlx_examples_results.txt
18+
-go vet -vettool=$(BIN)/sqlclosecheck ./testdata/sqlx_examples
19+
-go vet -vettool=$(BIN)/sqlclosecheck ./testdata/sqlx_examples 2> sqlx_examples_results.txt
1520
diff -a sqlx_examples_results.txt ./testdata/sqlx_examples/expected_results.txt
1621

1722
lint:

pkg/analyzer/analyzer.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ func getAction(instr ssa.Instruction, targetTypes []*types.Pointer) action {
241241
case *ssa.MakeInterface:
242242
return actionPassed
243243
case *ssa.Store:
244+
// A Row/Stmt is stored in a struct, which may be closed later
245+
// by a different flow.
246+
if _, ok := instr.Addr.(*ssa.FieldAddr); ok {
247+
return actionReturned
248+
}
249+
244250
if len(*instr.Addr.Referrers()) == 0 {
245251
return actionNoOp
246252
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package sqlx_examples
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/jmoiron/sqlx"
8+
)
9+
10+
type Server struct {
11+
Stmt *sqlx.Stmt
12+
}
13+
14+
func (s *Server) Close() {
15+
s.Stmt.Close()
16+
}
17+
18+
func (s Server) CloseInOtherFunc() {
19+
stmt, err := db.Preparex("SELECT 1")
20+
if err != nil {
21+
log.Fatal(err)
22+
}
23+
24+
s.Stmt = stmt
25+
26+
rows := stmt.QueryRow()
27+
fmt.Printf("%v", rows)
28+
29+
s.Close()
30+
}

0 commit comments

Comments
 (0)