aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/xml/typeinfo.go
diff options
context:
space:
mode:
authorDaniel Martí <mvdan@mvdan.cc>2019-09-24 18:14:10 +0100
committerDaniel Martí <mvdan@mvdan.cc>2020-05-28 22:48:53 +0000
commit8f4151ea67e1d498e0880f28d3fd803dc2c5448f (patch)
tree9f5395a39da32e095bc3a3d828bc434edb3966d3 /src/encoding/xml/typeinfo.go
parent107ebb178176f00c988a40943446af6f672b1e63 (diff)
downloadgo-8f4151ea67e1d498e0880f28d3fd803dc2c5448f.tar.xz
encoding/xml: only initialize nil struct fields when decoding
fieldInfo.value used to initialize nil anonymous struct fields if they were encountered. This behavior is wanted when decoding, but not when encoding. When encoding, the value should never be modified, and these nil fields should be skipped entirely. To fix the bug, add a bool argument to the function which tells the code whether we are encoding or decoding. Finally, add a couple of tests to cover the edge cases pointed out in the original issue. Fixes #27240. Change-Id: Ic97ae4bfe5f2062c8518e03d1dec07c3875e18f6 Reviewed-on: https://go-review.googlesource.com/c/go/+/196809 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Diffstat (limited to 'src/encoding/xml/typeinfo.go')
-rw-r--r--src/encoding/xml/typeinfo.go16
1 files changed, 13 insertions, 3 deletions
diff --git a/src/encoding/xml/typeinfo.go b/src/encoding/xml/typeinfo.go
index 639952c74a..f30fe58590 100644
--- a/src/encoding/xml/typeinfo.go
+++ b/src/encoding/xml/typeinfo.go
@@ -344,15 +344,25 @@ func (e *TagPathError) Error() string {
return fmt.Sprintf("%s field %q with tag %q conflicts with field %q with tag %q", e.Struct, e.Field1, e.Tag1, e.Field2, e.Tag2)
}
+const (
+ initNilPointers = true
+ dontInitNilPointers = false
+)
+
// value returns v's field value corresponding to finfo.
-// It's equivalent to v.FieldByIndex(finfo.idx), but initializes
-// and dereferences pointers as necessary.
-func (finfo *fieldInfo) value(v reflect.Value) reflect.Value {
+// It's equivalent to v.FieldByIndex(finfo.idx), but when passed
+// initNilPointers, it initializes and dereferences pointers as necessary.
+// When passed dontInitNilPointers and a nil pointer is reached, the function
+// returns a zero reflect.Value.
+func (finfo *fieldInfo) value(v reflect.Value, shouldInitNilPointers bool) reflect.Value {
for i, x := range finfo.idx {
if i > 0 {
t := v.Type()
if t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct {
if v.IsNil() {
+ if !shouldInitNilPointers {
+ return reflect.Value{}
+ }
v.Set(reflect.New(v.Type().Elem()))
}
v = v.Elem()