Skip to content

Commit 9a9a0fc

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Joe Tsai <[email protected]>
1 parent 6e9e9df commit 9a9a0fc

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

src/cmd/vet/all/whitelist/all.txt

+25
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,28 @@ cmd/link/link_test.go: struct field tag "\n\tLondon. Michaelmas term lately over
5454
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
5555
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
5656
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
57+
58+
// Tests of how xml handles spaces in struct tags.
59+
encoding/xml/marshal_test.go: struct field tag `xml:"ns foo"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
60+
encoding/xml/marshal_test.go: struct field tag `xml:"testns outer"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
61+
encoding/xml/marshal_test.go: struct field tag `xml:"outerns test"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
62+
encoding/xml/marshal_test.go: struct field tag `xml:"outerns test"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
63+
encoding/xml/marshal_test.go: struct field tag `xml:"space top"` not compatible with reflect.StructTag.Get: suspicious space found in struct tag value
64+
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
65+
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
66+
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
67+
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
68+
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
69+
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
70+
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
71+
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
72+
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
73+
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
74+
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
75+
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
76+
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
77+
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
78+
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
79+
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
80+
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
81+
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

src/cmd/vet/structtag.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"reflect"
1414
"strconv"
1515
"strings"
16+
"unicode"
1617
)
1718

1819
func init() {
@@ -31,6 +32,7 @@ func checkStructFieldTags(f *File, node ast.Node) {
3132
}
3233

3334
var checkTagDups = []string{"json", "xml"}
35+
var checkTagSpaces = map[string]bool{"json": true, "xml": true, "asn1": true}
3436

3537
// checkCanonicalFieldTag checks a single struct field tag.
3638
func checkCanonicalFieldTag(f *File, field *ast.Field, seen *map[[2]string]token.Pos) {
@@ -114,6 +116,7 @@ var (
114116
errTagSyntax = errors.New("bad syntax for struct tag pair")
115117
errTagKeySyntax = errors.New("bad syntax for struct tag key")
116118
errTagValueSyntax = errors.New("bad syntax for struct tag value")
119+
errTagValueSpace = errors.New("suspicious space found in struct tag value")
117120
errTagSpace = errors.New("key:\"value\" pairs not separated by spaces")
118121
)
119122

@@ -157,6 +160,7 @@ func validateStructTag(tag string) error {
157160
if tag[i+1] != '"' {
158161
return errTagValueSyntax
159162
}
163+
key := tag[:i]
160164
tag = tag[i+1:]
161165

162166
// Scan quoted string to find value.
@@ -173,9 +177,27 @@ func validateStructTag(tag string) error {
173177
qvalue := tag[:i+1]
174178
tag = tag[i+1:]
175179

176-
if _, err := strconv.Unquote(qvalue); err != nil {
180+
value, err := strconv.Unquote(qvalue)
181+
if err != nil {
177182
return errTagValueSyntax
178183
}
184+
185+
if !checkTagSpaces[key] {
186+
continue
187+
}
188+
189+
if key == "json" {
190+
// JSON allows using spaces in the name, so skip it.
191+
comma := strings.IndexRune(value, ',')
192+
if comma < 0 {
193+
continue
194+
}
195+
value = value[comma+1:]
196+
}
197+
198+
if strings.IndexFunc(value, unicode.IsSpace) >= 0 {
199+
return errTagValueSpace
200+
}
179201
}
180202
return nil
181203
}

src/cmd/vet/testdata/structtag.go

+15
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,18 @@ type DuplicateJSONFields struct {
8080
AnonymousXML `xml:"b,attr"` // ERROR "struct field AnonymousXML repeats xml attribute tag .b. also at testdata/structtag.go:76"
8181
}
8282
}
83+
84+
type UnexpectedSpacetest struct {
85+
A int `json:"a,omitempty"`
86+
B int `json:"b, omitempty"` // ERROR "suspicious space found in struct tag value"
87+
C int `json:"c,omitempty\t"` // ERROR "suspicious space found in struct tag value"
88+
D int `json:"d ,omitempty"`
89+
E int `json:"e,omitempty, string"` // ERROR "suspicious space found in struct tag value"
90+
F int `xml:" f"` // ERROR "suspicious space found in struct tag value"
91+
G int `xml:"g "` // ERROR "suspicious space found in struct tag value"
92+
H int `xml:"h ,omitempty"` // ERROR "suspicious space found in struct tag value"
93+
I int `xml:" i"` // ERROR "suspicious space found in struct tag value"
94+
J int `xml:"j "` // ERROR "suspicious space found in struct tag value"
95+
K int `xml:"k ,omitempty"` // ERROR "suspicious space found in struct tag value"
96+
L int `foo:" doesn't care "`
97+
}

0 commit comments

Comments
 (0)