Skip to content

Commit be08ddb

Browse files
SamWhitedianlancetaylor
authored andcommitted
encoding/xml: don't panic when custom Unmarshaler sees StartElement
Change-Id: I90aa0a983abd0080f3de75d3340fdb15c1f9ca35 Reviewed-on: https://go-review.googlesource.com/70891 Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Sam Whited <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 01c144c commit be08ddb

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ encoding/xml/marshal.go: method MarshalXML(e *Encoder, start StartElement) error
4848
encoding/xml/marshal_test.go: method MarshalXML(e *Encoder, start StartElement) error should have signature MarshalXML(*xml.Encoder, xml.StartElement) error
4949
encoding/xml/read.go: method UnmarshalXML(d *Decoder, start StartElement) error should have signature UnmarshalXML(*xml.Decoder, xml.StartElement) error
5050
encoding/xml/read_test.go: method UnmarshalXML(d *Decoder, start StartElement) error should have signature UnmarshalXML(*xml.Decoder, xml.StartElement) error
51+
encoding/xml/xml_test.go: method UnmarshalXML(*Decoder, StartElement) error should have signature UnmarshalXML(*xml.Decoder, xml.StartElement) error
5152

5253
// Long struct tags used to test reflect internals
5354
cmd/link/link_test.go: struct field tag "\n\tLondon. Michaelmas term lately over, and the Lord Chancellor sitting in Lincoln’s Inn Hall. Implacable November weather. As much mud in the streets as if the waters had but newly retired from the face of the earth, and it would not be wonderful to meet a Megalosaurus, forty feet long or so, waddling like an elephantine lizard up Holborn Hill. Smoke lowering down from chimney-pots, making a soft black drizzle, with flakes of soot in it as big as full-grown snowflakes—gone into mourning, one might imagine, for the death of the sun. Dogs, undistinguishable in mire. Horses, scarcely better; splashed to their very blinkers. Foot passengers, jostling one another’s umbrellas in a general infection of ill temper, and losing their foot-hold at street-corners, where tens of thousands of other foot passengers have been slipping and sliding since the day broke (if this day ever broke), adding new deposits to the crust upon crust of mud, sticking at those points tenaciously to the pavement, and accumulating at compound interest.\n\n\tFog everywhere. Fog up the river, where it flows among green aits and meadows; fog down the river, where it rolls defiled among the tiers of shipping and the waterside pollutions of a great (and dirty) city. Fog on the Essex marshes, fog on the Kentish heights. Fog creeping into the cabooses of collier-brigs; fog lying out on the yards and hovering in the rigging of great ships; fog drooping on the gunwales of barges and small boats. Fog in the eyes and throats of ancient Greenwich pensioners, wheezing by the firesides of their wards; fog in the stem and bowl of the afternoon pipe of the wrathful skipper, down in his close cabin; fog cruelly pinching the toes and fingers of his shivering little ‘prentice boy on deck. Chance people on the bridges peeping over the parapets into a nether sky of fog, with fog all round them, as if they were up in a balloon and hanging in the misty clouds.\n\n\tGas looming through the fog in divers places in the streets, much as the sun may, from the spongey fields, be seen to loom by husbandman and ploughboy. Most of the shops lighted two hours before their time—as the gas seems to know, for it has a haggard and unwilling look.\n\n\tThe raw afternoon is rawest, and the dense fog is densest, and the muddy streets are muddiest near that leaden-headed old obstruction, appropriate ornament for the threshold of a leaden-headed old corporation, Temple Bar. And hard by Temple Bar, in Lincoln’s Inn Hall, at the very heart of the fog, sits the Lord High Chancellor in his High Court of Chancery." not compatible with reflect.StructTag.Get: bad syntax for struct tag key

src/encoding/xml/xml.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,6 @@ func NewTokenDecoder(t TokenReader) *Decoder {
277277
// If Token encounters an unrecognized name space prefix,
278278
// it uses the prefix as the Space rather than report an error.
279279
func (d *Decoder) Token() (Token, error) {
280-
if d.t != nil {
281-
return d.t.Token()
282-
}
283280
var t Token
284281
var err error
285282
if d.stk != nil && d.stk.kind == stkEOF {
@@ -548,6 +545,9 @@ func (d *Decoder) RawToken() (Token, error) {
548545
}
549546

550547
func (d *Decoder) rawToken() (Token, error) {
548+
if d.t != nil {
549+
return d.t.Token()
550+
}
551551
if d.err != nil {
552552
return nil, d.err
553553
}

src/encoding/xml/xml_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,3 +861,26 @@ func TestWrapDecoder(t *testing.T) {
861861
t.Fatalf("Got unexpected chardata: `%s`\n", o.Chardata)
862862
}
863863
}
864+
865+
type tokReader struct{}
866+
867+
func (tokReader) Token() (Token, error) {
868+
return StartElement{}, nil
869+
}
870+
871+
type Failure struct{}
872+
873+
func (Failure) UnmarshalXML(*Decoder, StartElement) error {
874+
return nil
875+
}
876+
877+
func TestTokenUnmarshaler(t *testing.T) {
878+
defer func() {
879+
if r := recover(); r != nil {
880+
t.Error("Unexpected panic using custom token unmarshaler")
881+
}
882+
}()
883+
884+
d := NewTokenDecoder(tokReader{})
885+
d.Decode(&Failure{})
886+
}

0 commit comments

Comments
 (0)