Skip to content

Commit

Permalink
Merge branch 'main' of github.com:ryanrolds/sqlclosecheck into main
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanrolds committed Jan 29, 2023
2 parents bb94246 + c2dc47e commit 10544ad
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
15 changes: 10 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
BIN := bin

PHONY: build install test

build:
go build
$(BIN):
mkdir -p $@

build: $(BIN)
go build -o $(BIN)/sqlclosecheck .

install:
go install

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

lint:
Expand Down
6 changes: 6 additions & 0 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ func getAction(instr ssa.Instruction, targetTypes []*types.Pointer) action {
case *ssa.MakeInterface:
return actionPassed
case *ssa.Store:
// A Row/Stmt is stored in a struct, which may be closed later
// by a different flow.
if _, ok := instr.Addr.(*ssa.FieldAddr); ok {
return actionReturned
}

if len(*instr.Addr.Referrers()) == 0 {
return actionNoOp
}
Expand Down
30 changes: 30 additions & 0 deletions testdata/sqlx_examples/close_in_other_func.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package sqlx_examples

import (
"fmt"
"log"

"github.com/jmoiron/sqlx"
)

type Server struct {
Stmt *sqlx.Stmt
}

func (s *Server) Close() {
s.Stmt.Close()
}

func (s Server) CloseInOtherFunc() {
stmt, err := db.Preparex("SELECT 1")
if err != nil {
log.Fatal(err)
}

s.Stmt = stmt

rows := stmt.QueryRow()
fmt.Printf("%v", rows)

s.Close()
}

0 comments on commit 10544ad

Please sign in to comment.