Commit 9a9a0fcc authored by Francesc Campoy's avatar Francesc Campoy Committed by Francesc Campoy Flores

cmd/vet: diagnose xml/json tag with space after comma

Fixes #19520

Change-Id: Ib0a1f0f2429172d2781c9b6fbd21f9cb8da1b8ed
Reviewed-on: https://go-review.googlesource.com/43295
Run-TryBot: Francesc Campoy Flores <campoy@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarJoe Tsai <thebrokentoaster@gmail.com>
parent 6e9e9dfa
......@@ -54,3 +54,28 @@ cmd/link/link_test.go: struct field tag "\n\tLondon. Michaelmas term lately over
cmd/link/link_test.go: struct field tag "\n\tIt was grand to see how the wind awoke, and bent the trees, and drove the rain before it like a cloud of smoke; and to hear the solemn thunder, and to see the lightning; and while thinking with awe of the tremendous powers by which our little lives are encompassed, to consider how beneficent they are, and how upon the smallest flower and leaf there was already a freshness poured from all this seeming rage, which seemed to make creation new again." not compatible with reflect.StructTag.Get: bad syntax for struct tag key
cmd/link/link_test.go: struct field tag "\n\tJarndyce and Jarndyce drones on. This scarecrow of a suit has, over the course of time, become so complicated, that no man alive knows what it means. The parties to it understand it least; but it has been observed that no two Chancery lawyers can talk about it for five minutes, without coming to a total disagreement as to all the premises. Innumerable children have been born into the cause; innumerable young people have married into it; innumerable old people have died out of it. Scores of persons have deliriously found themselves made parties in Jarndyce and Jarndyce, without knowing how or why; whole families have inherited legendary hatreds with the suit. The little plaintiff or defendant, who was promised a new rocking-horse when Jarndyce and Jarndyce should be settled, has grown up, possessed himself of a real horse, and trotted away into the other world. Fair wards of court have faded into mothers and grandmothers; a long procession of Chancellors has come in and gone out; the legion of bills in the suit have been transformed into mere bills of mortality; there are not three Jarndyces left upon the earth perhaps, since old Tom Jarndyce in despair blew his brains out at a coffee-house in Chancery Lane; but Jarndyce and Jarndyce still drags its dreary length before the Court, perennially hopeless." not compatible with reflect.StructTag.Get: bad syntax for struct tag key
cmd/link/link_test.go: struct field tag "\n\tThe one great principle of the English law is, to make business for itself. There is no other principle distinctly, certainly, and consistently maintained through all its narrow turnings. Viewed by this light it becomes a coherent scheme, and not the monstrous maze the laity are apt to think it. Let them but once clearly perceive that its grand principle is to make business for itself at their expense, and surely they will cease to grumble." not compatible with reflect.StructTag.Get: bad syntax for struct tag key
// Tests of how xml handles spaces in struct tags.
encoding/xml/marshal_test.go: struct field tag `xml:"ns foo"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
encoding/xml/marshal_test.go: struct field tag `xml:"testns outer"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
encoding/xml/marshal_test.go: struct field tag `xml:"outerns test"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
encoding/xml/marshal_test.go: struct field tag `xml:"outerns test"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
encoding/xml/marshal_test.go: struct field tag `xml:"space top"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
encoding/xml/marshal_test.go: struct field tag `xml:"space x>c"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
encoding/xml/marshal_test.go: struct field tag `xml:"space1 x>c"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
encoding/xml/marshal_test.go: struct field tag `xml:"space1 x>d"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
encoding/xml/marshal_test.go: struct field tag `xml:"space x>c"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
encoding/xml/marshal_test.go: struct field tag `xml:"space1 x>c"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
encoding/xml/marshal_test.go: struct field tag `xml:"space1 x>d"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
encoding/xml/marshal_test.go: struct field tag `xml:"space x>b"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
encoding/xml/marshal_test.go: struct field tag `xml:"space1 x>b"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
encoding/xml/read_test.go: struct field tag `xml:"http://www.w3.org/2005/Atom feed"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
encoding/xml/read_test.go: struct field tag `xml:"http://www.w3.org/TR/html4/ table"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
encoding/xml/read_test.go: struct field tag `xml:"http://www.w3schools.com/furniture table"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
encoding/xml/read_test.go: struct field tag `xml:"http://www.w3.org/TR/html4/ table,attr"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
encoding/xml/read_test.go: struct field tag `xml:"http://www.w3schools.com/furniture table,attr"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
encoding/xml/read_test.go: struct field tag `xml:"http://www.w3.org/XML/1998/namespace lang,attr,omitempty"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
encoding/xml/read_test.go: struct field tag `xml:"http://golang.org/xml/ other,attr,omitempty"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
encoding/xml/read_test.go: struct field tag `xml:"http://golang.org/xmlfoo/ other,attr,omitempty"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
encoding/xml/read_test.go: struct field tag `xml:"http://golang.org/json/ other,attr,omitempty"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
encoding/xml/read_test.go: struct field tag `xml:"http://golang.org/2/json/ other,attr,omitempty"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
\ No newline at end of file
......@@ -13,6 +13,7 @@ import (
"reflect"
"strconv"
"strings"
"unicode"
)
func init() {
......@@ -31,6 +32,7 @@ func checkStructFieldTags(f *File, node ast.Node) {
}
var checkTagDups = []string{"json", "xml"}
var checkTagSpaces = map[string]bool{"json": true, "xml": true, "asn1": true}
// checkCanonicalFieldTag checks a single struct field tag.
func checkCanonicalFieldTag(f *File, field *ast.Field, seen *map[[2]string]token.Pos) {
......@@ -114,6 +116,7 @@ var (
errTagSyntax = errors.New("bad syntax for struct tag pair")
errTagKeySyntax = errors.New("bad syntax for struct tag key")
errTagValueSyntax = errors.New("bad syntax for struct tag value")
errTagValueSpace = errors.New("suspicious space found in struct tag value")
errTagSpace = errors.New("key:\"value\" pairs not separated by spaces")
)
......@@ -157,6 +160,7 @@ func validateStructTag(tag string) error {
if tag[i+1] != '"' {
return errTagValueSyntax
}
key := tag[:i]
tag = tag[i+1:]
// Scan quoted string to find value.
......@@ -173,9 +177,27 @@ func validateStructTag(tag string) error {
qvalue := tag[:i+1]
tag = tag[i+1:]
if _, err := strconv.Unquote(qvalue); err != nil {
value, err := strconv.Unquote(qvalue)
if err != nil {
return errTagValueSyntax
}
if !checkTagSpaces[key] {
continue
}
if key == "json" {
// JSON allows using spaces in the name, so skip it.
comma := strings.IndexRune(value, ',')
if comma < 0 {
continue
}
value = value[comma+1:]
}
if strings.IndexFunc(value, unicode.IsSpace) >= 0 {
return errTagValueSpace
}
}
return nil
}
......@@ -80,3 +80,18 @@ type DuplicateJSONFields struct {
AnonymousXML `xml:"b,attr"` // ERROR "struct field AnonymousXML repeats xml attribute tag .b. also at testdata/structtag.go:76"
}
}
type UnexpectedSpacetest struct {
A int `json:"a,omitempty"`
B int `json:"b, omitempty"` // ERROR "suspicious space found in struct tag value"
C int `json:"c,omitempty\t"` // ERROR "suspicious space found in struct tag value"
D int `json:"d ,omitempty"`
E int `json:"e,omitempty, string"` // ERROR "suspicious space found in struct tag value"
F int `xml:" f"` // ERROR "suspicious space found in struct tag value"
G int `xml:"g "` // ERROR "suspicious space found in struct tag value"
H int `xml:"h ,omitempty"` // ERROR "suspicious space found in struct tag value"
I int `xml:" i"` // ERROR "suspicious space found in struct tag value"
J int `xml:"j "` // ERROR "suspicious space found in struct tag value"
K int `xml:"k ,omitempty"` // ERROR "suspicious space found in struct tag value"
L int `foo:" doesn't care "`
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment