go/analysis/passes/unmarshal: port vet's unmarshal checker
The checker has been modified to avoid making two memory allocations for every CallExpr in the program. Originally: https://go-review.googlesource.com/c/139997 Updates golang/go#27564 Change-Id: I168869272a1d78d47d84c049aba619bb223cad70 Reviewed-on: https://go-review.googlesource.com/c/148562 Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
parent
77439c5518
commit
879803d4ad
|
@ -33,6 +33,7 @@ import (
|
|||
"golang.org/x/tools/go/analysis/passes/stdmethods"
|
||||
"golang.org/x/tools/go/analysis/passes/structtag"
|
||||
"golang.org/x/tools/go/analysis/passes/tests"
|
||||
"golang.org/x/tools/go/analysis/passes/unmarshal"
|
||||
"golang.org/x/tools/go/analysis/passes/unreachable"
|
||||
"golang.org/x/tools/go/analysis/passes/unsafeptr"
|
||||
"golang.org/x/tools/go/analysis/passes/unusedresult"
|
||||
|
@ -59,6 +60,7 @@ var analyzers = []*analysis.Analyzer{
|
|||
stdmethods.Analyzer,
|
||||
structtag.Analyzer,
|
||||
tests.Analyzer,
|
||||
unmarshal.Analyzer,
|
||||
unreachable.Analyzer,
|
||||
unsafeptr.Analyzer,
|
||||
unusedresult.Analyzer,
|
||||
|
|
|
@ -37,6 +37,7 @@ import (
|
|||
"golang.org/x/tools/go/analysis/passes/stdmethods"
|
||||
"golang.org/x/tools/go/analysis/passes/structtag"
|
||||
"golang.org/x/tools/go/analysis/passes/tests"
|
||||
"golang.org/x/tools/go/analysis/passes/unmarshal"
|
||||
"golang.org/x/tools/go/analysis/passes/unreachable"
|
||||
"golang.org/x/tools/go/analysis/passes/unsafeptr"
|
||||
"golang.org/x/tools/go/analysis/passes/unusedresult"
|
||||
|
@ -64,6 +65,7 @@ func main() {
|
|||
stdmethods.Analyzer,
|
||||
structtag.Analyzer,
|
||||
tests.Analyzer,
|
||||
unmarshal.Analyzer,
|
||||
unreachable.Analyzer,
|
||||
unsafeptr.Analyzer,
|
||||
unusedresult.Analyzer,
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
// The unmarshal command runs the unmarshal analyzer.
|
||||
package main
|
||||
|
||||
import (
|
||||
"golang.org/x/tools/go/analysis/passes/unmarshal"
|
||||
"golang.org/x/tools/go/analysis/singlechecker"
|
||||
)
|
||||
|
||||
func main() { singlechecker.Main(unmarshal.Analyzer) }
|
|
@ -0,0 +1,58 @@
|
|||
// Copyright 2018 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// This file contains tests for the unmarshal checker.
|
||||
|
||||
package testdata
|
||||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"io"
|
||||
)
|
||||
|
||||
func _() {
|
||||
type t struct {
|
||||
a int
|
||||
}
|
||||
var v t
|
||||
var r io.Reader
|
||||
|
||||
json.Unmarshal([]byte{}, v) // want "call of Unmarshal passes non-pointer as second argument"
|
||||
json.Unmarshal([]byte{}, &v)
|
||||
json.NewDecoder(r).Decode(v) // want "call of Decode passes non-pointer"
|
||||
json.NewDecoder(r).Decode(&v)
|
||||
gob.NewDecoder(r).Decode(v) // want "call of Decode passes non-pointer"
|
||||
gob.NewDecoder(r).Decode(&v)
|
||||
xml.Unmarshal([]byte{}, v) // want "call of Unmarshal passes non-pointer as second argument"
|
||||
xml.Unmarshal([]byte{}, &v)
|
||||
xml.NewDecoder(r).Decode(v) // want "call of Decode passes non-pointer"
|
||||
xml.NewDecoder(r).Decode(&v)
|
||||
|
||||
var p *t
|
||||
json.Unmarshal([]byte{}, p)
|
||||
json.Unmarshal([]byte{}, *p) // want "call of Unmarshal passes non-pointer as second argument"
|
||||
json.NewDecoder(r).Decode(p)
|
||||
json.NewDecoder(r).Decode(*p) // want "call of Decode passes non-pointer"
|
||||
gob.NewDecoder(r).Decode(p)
|
||||
gob.NewDecoder(r).Decode(*p) // want "call of Decode passes non-pointer"
|
||||
xml.Unmarshal([]byte{}, p)
|
||||
xml.Unmarshal([]byte{}, *p) // want "call of Unmarshal passes non-pointer as second argument"
|
||||
xml.NewDecoder(r).Decode(p)
|
||||
xml.NewDecoder(r).Decode(*p) // want "call of Decode passes non-pointer"
|
||||
|
||||
var i interface{}
|
||||
json.Unmarshal([]byte{}, i)
|
||||
json.NewDecoder(r).Decode(i)
|
||||
|
||||
json.Unmarshal([]byte{}, nil) // want "call of Unmarshal passes non-pointer as second argument"
|
||||
json.Unmarshal([]byte{}, []t{}) // want "call of Unmarshal passes non-pointer as second argument"
|
||||
json.Unmarshal([]byte{}, map[string]int{}) // want "call of Unmarshal passes non-pointer as second argument"
|
||||
json.NewDecoder(r).Decode(nil) // want "call of Decode passes non-pointer"
|
||||
json.NewDecoder(r).Decode([]t{}) // want "call of Decode passes non-pointer"
|
||||
json.NewDecoder(r).Decode(map[string]int{}) // want "call of Decode passes non-pointer"
|
||||
|
||||
json.Unmarshal(func() ([]byte, interface{}) { return []byte{}, v }())
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
// Copyright 2018 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// The unmarshal package defines an Analyzer that checks for passing
|
||||
// non-pointer or non-interface types to unmarshal and decode functions.
|
||||
package unmarshal
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"go/types"
|
||||
|
||||
"golang.org/x/tools/go/analysis"
|
||||
"golang.org/x/tools/go/analysis/passes/inspect"
|
||||
"golang.org/x/tools/go/ast/inspector"
|
||||
"golang.org/x/tools/go/types/typeutil"
|
||||
)
|
||||
|
||||
const doc = `report passing non-pointer or non-interface values to unmarshal
|
||||
|
||||
The unmarshal analysis reports calls to functions such as json.Unmarshal
|
||||
in which the argument type is not a pointer or an interface.`
|
||||
|
||||
var Analyzer = &analysis.Analyzer{
|
||||
Name: "unmarshal",
|
||||
Doc: doc,
|
||||
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
||||
Run: run,
|
||||
}
|
||||
|
||||
func run(pass *analysis.Pass) (interface{}, error) {
|
||||
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
|
||||
|
||||
nodeFilter := []ast.Node{
|
||||
(*ast.CallExpr)(nil),
|
||||
}
|
||||
inspect.Preorder(nodeFilter, func(n ast.Node) {
|
||||
call := n.(*ast.CallExpr)
|
||||
fn := typeutil.StaticCallee(pass.TypesInfo, call)
|
||||
if fn == nil {
|
||||
return // not a static call
|
||||
}
|
||||
|
||||
// Classify the callee (without allocating memory).
|
||||
argidx := -1
|
||||
recv := fn.Type().(*types.Signature).Recv()
|
||||
if fn.Name() == "Unmarshal" && recv == nil {
|
||||
// "encoding/json".Unmarshal
|
||||
// "encoding/xml".Unmarshal
|
||||
switch fn.Pkg().Path() {
|
||||
case "encoding/json", "encoding/xml":
|
||||
argidx = 1 // func([]byte, interface{})
|
||||
}
|
||||
} else if fn.Name() == "Decode" && recv != nil {
|
||||
// (*"encoding/json".Decoder).Decode
|
||||
// (* "encoding/gob".Decoder).Decode
|
||||
// (* "encoding/xml".Decoder).Decode
|
||||
t := recv.Type()
|
||||
if ptr, ok := t.(*types.Pointer); ok {
|
||||
t = ptr.Elem()
|
||||
}
|
||||
tname := t.(*types.Named).Obj()
|
||||
if tname.Name() == "Decoder" {
|
||||
switch tname.Pkg().Path() {
|
||||
case "encoding/json", "encoding/xml", "encoding/gob":
|
||||
argidx = 0 // func(interface{})
|
||||
}
|
||||
}
|
||||
}
|
||||
if argidx < 0 {
|
||||
return // not a function we are interested in
|
||||
}
|
||||
|
||||
if len(call.Args) < argidx+1 {
|
||||
return // not enough arguments, e.g. called with return values of another function
|
||||
}
|
||||
|
||||
t := pass.TypesInfo.Types[call.Args[argidx]].Type
|
||||
switch t.Underlying().(type) {
|
||||
case *types.Pointer, *types.Interface:
|
||||
return
|
||||
}
|
||||
|
||||
switch argidx {
|
||||
case 0:
|
||||
pass.Reportf(call.Lparen, "call of %s passes non-pointer", fn.Name())
|
||||
case 1:
|
||||
pass.Reportf(call.Lparen, "call of %s passes non-pointer as second argument", fn.Name())
|
||||
}
|
||||
})
|
||||
return nil, nil
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
// Copyright 2018 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package unmarshal_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"golang.org/x/tools/go/analysis/analysistest"
|
||||
"golang.org/x/tools/go/analysis/passes/unmarshal"
|
||||
)
|
||||
|
||||
func Test(t *testing.T) {
|
||||
testdata := analysistest.TestData()
|
||||
analysistest.Run(t, testdata, unmarshal.Analyzer, "a")
|
||||
}
|
Loading…
Reference in New Issue