Skip to content

Commit

Permalink
Find uses of Rows.Err when Rows is an interface (#23)
Browse files Browse the repository at this point in the history
* Update dependencies so that analysis works on Go 1.16

* Find uses of Rows.Err when Rows is an interface
  • Loading branch information
coder543 committed Oct 18, 2021
1 parent d907ca7 commit bd4dd44
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 68 deletions.
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ module github.com/jingyugao/rowserrcheck

go 1.13

require (
github.com/gostaticanalysis/analysisutil v0.0.0-20190318220348-4088753ea4d3
golang.org/x/tools v0.0.0-20190322203728-c1a832b0ad89
)
require golang.org/x/tools v0.1.1
30 changes: 24 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
github.com/gostaticanalysis/analysisutil v0.0.0-20190318220348-4088753ea4d3 h1:JVnpOZS+qxli+rgVl98ILOXVNbW+kb5wcxeGx8ShUIw=
github.com/gostaticanalysis/analysisutil v0.0.0-20190318220348-4088753ea4d3/go.mod h1:eEOZF4jCKGi+aprrirO9e7WKB3beBRtWgqGunKl6pKE=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190311215038-5c2858a9cfe5/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190322203728-c1a832b0ad89 h1:iWXXYN3edZ3Nd/7I6Rt1sXrWVmhF9bgVtlEJ7BbH124=
golang.org/x/tools v0.0.0-20190322203728-c1a832b0ad89/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.1 h1:wGiQel/hW0NnEkJUk8lbzkX2gFJU6PFxf1v5OlCfuOs=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
89 changes: 31 additions & 58 deletions passes/rowserr/rowserr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package rowserr
import (
"go/ast"
"go/types"
"strconv"

"github.com/gostaticanalysis/analysisutil"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/buildssa"
"golang.org/x/tools/go/ssa"
Expand All @@ -29,11 +27,12 @@ const (
)

type runner struct {
pass *analysis.Pass
rowsTyp *types.Pointer
rowsObj types.Object
skipFile map[*ast.File]bool
sqlPkgs []string
pass *analysis.Pass
rowsTyp *types.Pointer
rowsInterface *types.Interface
rowsObj types.Object
skipFile map[*ast.File]bool
sqlPkgs []string
}

func NewRun(pkgs ...string) func(pass *analysis.Pass) (interface{}, error) {
Expand Down Expand Up @@ -66,7 +65,6 @@ func (r runner) run(pass *analysis.Pass, pkgPath string) {
// skip checking
return
}

r.rowsObj = rowsType.Object()
if r.rowsObj == nil {
// skip checking
Expand All @@ -78,15 +76,15 @@ func (r runner) run(pass *analysis.Pass, pkgPath string) {
return
}

rowsInterface, ok := r.rowsObj.Type().Underlying().(*types.Interface)
if ok {
r.rowsInterface = rowsInterface
}

r.rowsTyp = types.NewPointer(resNamed)
r.skipFile = map[*ast.File]bool{}

for _, f := range funcs {
if r.noImportedDBSQL(f) {
// skip this
continue
}

// skip if the function is just referenced
var isRefFunc bool

Expand Down Expand Up @@ -139,10 +137,6 @@ func (r *runner) errCallMissing(b *ssa.BasicBlock, i int) (ret bool) {
switch c := aref.(type) {
case *ssa.MakeClosure:
f := c.Fn.(*ssa.Function)
if r.noImportedDBSQL(f) {
// skip this
continue
}
called := r.isClosureCalled(c)
if r.calledInFunc(f, called) {
return true
Expand Down Expand Up @@ -203,17 +197,18 @@ func (r *runner) getCallReturnsRow(instr ssa.Instruction) (*ssa.Call, bool) {
}

res := call.Call.Signature().Results()
flag := false

for i := 0; i < res.Len(); i++ {
flag = flag || types.Identical(res.At(i).Type(), r.rowsTyp)
}

if !flag {
return nil, false
typeToCheck := res.At(i).Type()
if types.Identical(typeToCheck, r.rowsTyp) {
return call, true
}
if r.rowsInterface != nil && types.Implements(typeToCheck, r.rowsInterface) {
return call, true
}
}

return call, true
return nil, false
}

func (r *runner) getRowsVal(instr ssa.Instruction) (ssa.Value, bool) {
Expand All @@ -222,10 +217,16 @@ func (r *runner) getRowsVal(instr ssa.Instruction) (ssa.Value, bool) {
if len(instr.Call.Args) == 1 && types.Identical(instr.Call.Args[0].Type(), r.rowsTyp) {
return instr.Call.Args[0], true
}
if len(instr.Call.Args) == 1 && r.rowsInterface != nil && types.Implements(instr.Call.Args[0].Type(), r.rowsInterface) {
return instr.Call.Args[0], true
}
case ssa.Value:
if types.Identical(instr.Type(), r.rowsTyp) {
return instr, true
}
if r.rowsInterface != nil && types.Implements(instr.Type(), r.rowsInterface) {
return instr, true
}
default:
}

Expand All @@ -250,10 +251,16 @@ func (r *runner) isErrCall(ccall ssa.Instruction) bool {
if ccall.Call.Value != nil && ccall.Call.Value.Name() == errMethod {
return true
}
if ccall.Call.Method != nil && ccall.Call.Method.Name() == errMethod {
return true
}
case *ssa.Call:
if ccall.Call.Value != nil && ccall.Call.Value.Name() == errMethod {
return true
}
if ccall.Call.Method != nil && ccall.Call.Method.Name() == errMethod {
return true
}
}

return false
Expand All @@ -270,40 +277,6 @@ func (r *runner) isClosureCalled(c *ssa.MakeClosure) bool {
return false
}

func (r *runner) noImportedDBSQL(f *ssa.Function) (ret bool) {
obj := f.Object()
if obj == nil {
return false
}

file := analysisutil.File(r.pass, obj.Pos())
if file == nil {
return false
}

if skip, has := r.skipFile[file]; has {
return skip
}
defer func() {
r.skipFile[file] = ret
}()

for _, impt := range file.Imports {
path, err := strconv.Unquote(impt.Path.Value)
if err != nil {
continue
}
path = analysisutil.RemoveVendor(path)
for _, pkg := range r.sqlPkgs {
if pkg == path {
return false
}
}
}

return true
}

func (r *runner) calledInFunc(f *ssa.Function, called bool) bool {
for _, b := range f.Blocks {
for i, instr := range b.Instrs {
Expand Down

0 comments on commit bd4dd44

Please sign in to comment.