1
0
mirror of https://github.com/golang/go synced 2024-11-05 17:36:15 -07:00

go/analysis/passes/unmarshal: Add check for asn1.Unmarshal

Unmarshal package checks whether `Unmarshal` and(or) `Decode` call of `encoding/json`, `encoding/xml` and `encoding/gob` package is valid or not. However, it didn't check `encoding/asn1` package's one.

This change makes it check whether `asn1.Unmarshal` call is valid or not as well.

Change-Id: If32d243f3a82ebb5ca3ca80657f4616e5daced2f
GitHub-Last-Rev: e61a96941ee64d7d141e635b374fe07104751283
GitHub-Pull-Request: golang/tools#243
Reviewed-on: https://go-review.googlesource.com/c/tools/+/243397
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
KimMachineGun 2020-07-18 14:38:49 +00:00 committed by Michael Matloob
parent 6123e77877
commit 7041913083
2 changed files with 9 additions and 3 deletions

View File

@ -7,6 +7,7 @@
package testdata package testdata
import ( import (
"encoding/asn1"
"encoding/gob" "encoding/gob"
"encoding/json" "encoding/json"
"encoding/xml" "encoding/xml"
@ -30,6 +31,8 @@ func _() {
xml.Unmarshal([]byte{}, &v) xml.Unmarshal([]byte{}, &v)
xml.NewDecoder(r).Decode(v) // want "call of Decode passes non-pointer" xml.NewDecoder(r).Decode(v) // want "call of Decode passes non-pointer"
xml.NewDecoder(r).Decode(&v) xml.NewDecoder(r).Decode(&v)
asn1.Unmarshal([]byte{}, v) // want "call of Unmarshal passes non-pointer as second argument"
asn1.Unmarshal([]byte{}, &v)
var p *t var p *t
json.Unmarshal([]byte{}, p) json.Unmarshal([]byte{}, p)
@ -42,6 +45,8 @@ func _() {
xml.Unmarshal([]byte{}, *p) // want "call of Unmarshal passes non-pointer as second argument" xml.Unmarshal([]byte{}, *p) // want "call of Unmarshal passes non-pointer as second argument"
xml.NewDecoder(r).Decode(p) xml.NewDecoder(r).Decode(p)
xml.NewDecoder(r).Decode(*p) // want "call of Decode passes non-pointer" xml.NewDecoder(r).Decode(*p) // want "call of Decode passes non-pointer"
asn1.Unmarshal([]byte{}, p)
asn1.Unmarshal([]byte{}, *p) // want "call of Unmarshal passes non-pointer as second argument"
var i interface{} var i interface{}
json.Unmarshal([]byte{}, i) json.Unmarshal([]byte{}, i)

View File

@ -30,7 +30,7 @@ var Analyzer = &analysis.Analyzer{
func run(pass *analysis.Pass) (interface{}, error) { func run(pass *analysis.Pass) (interface{}, error) {
switch pass.Pkg.Path() { switch pass.Pkg.Path() {
case "encoding/gob", "encoding/json", "encoding/xml": case "encoding/gob", "encoding/json", "encoding/xml", "encoding/asn1":
// These packages know how to use their own APIs. // These packages know how to use their own APIs.
// Sometimes they are testing what happens to incorrect programs. // Sometimes they are testing what happens to incorrect programs.
return nil, nil return nil, nil
@ -53,9 +53,10 @@ func run(pass *analysis.Pass) (interface{}, error) {
recv := fn.Type().(*types.Signature).Recv() recv := fn.Type().(*types.Signature).Recv()
if fn.Name() == "Unmarshal" && recv == nil { if fn.Name() == "Unmarshal" && recv == nil {
// "encoding/json".Unmarshal // "encoding/json".Unmarshal
// "encoding/xml".Unmarshal // "encoding/xml".Unmarshal
// "encoding/asn1".Unmarshal
switch fn.Pkg().Path() { switch fn.Pkg().Path() {
case "encoding/json", "encoding/xml": case "encoding/json", "encoding/xml", "encoding/asn1":
argidx = 1 // func([]byte, interface{}) argidx = 1 // func([]byte, interface{})
} }
} else if fn.Name() == "Decode" && recv != nil { } else if fn.Name() == "Decode" && recv != nil {