Skip to content

Commit fa5c504

Browse files
committed
encoding/xml: truncate generic type names
xml names can't have any of '[],' in them, which might appear in generic type names. Truncate at the first '[' so the names are still valid. Fixes #48318 Change-Id: I110ff4269f763089467e7cf84b0f0c5075fb44b7 Reviewed-on: https://go-review.googlesource.com/c/go/+/349349 Trust: Keith Randall <[email protected]> Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent 13aa0d8 commit fa5c504

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/encoding/xml/marshal.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,10 @@ func (p *printer) marshalValue(val reflect.Value, finfo *fieldInfo, startTemplat
494494
}
495495
if start.Name.Local == "" {
496496
name := typ.Name()
497+
if i := strings.IndexByte(name, '['); i >= 0 {
498+
// Truncate generic instantiation name. See issue 48318.
499+
name = name[:i]
500+
}
497501
if name == "" {
498502
return &UnsupportedTypeError{typ}
499503
}

test/typeparam/issue48318.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// run -gcflags=-G=3
2+
3+
// Copyright 2021 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
import (
10+
"encoding/xml"
11+
"fmt"
12+
)
13+
14+
type A[T, U any] struct {
15+
Name T `xml:"name"`
16+
Data U `xml:"data"`
17+
}
18+
19+
func main() {
20+
src := &A[string, int]{Name: "name", Data: 1}
21+
data, err := xml.Marshal(src)
22+
if err != nil {
23+
panic(err)
24+
}
25+
dst := &A[string, int]{}
26+
err = xml.Unmarshal(data, dst)
27+
if err != nil {
28+
panic(err)
29+
}
30+
if *src != *dst {
31+
panic(fmt.Sprintf("wanted %#v got %#v", src, dst))
32+
}
33+
}

0 commit comments

Comments
 (0)