go/src/encoding
Daniel Martí 8f4151ea67 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>
2020-05-28 22:48:53 +00:00
..
ascii85 encoding/ascii85: make bigtest big again 2017-06-23 14:28:30 +00:00
asn1 encoding/asn1: document what Unmarshal returns in rest 2020-05-23 06:47:06 +00:00
base32 encoding/base32: increase performance and code reuse 2019-09-11 00:56:39 +00:00
base64 encoding/base64: improve performance up to 20% total 2020-04-22 21:36:41 +00:00
binary encoding/binary: add float support to fast path 2019-11-08 18:35:59 +00:00
csv encoding/csv: optimize Write by giving fieldNeedsQuotes a fast path for when Comma is ascii 2020-05-05 23:57:19 +00:00
gob all: fix a number of misuses of the word "an" 2019-12-10 16:23:10 +00:00
hex encoding/hex: remove unused variable from BenchmarkDump 2020-03-03 14:53:52 +00:00
json Revert "encoding/json: reuse values when decoding map elements" 2020-05-28 22:17:10 +00:00
pem all: remove PEM-encoded private keys from tests 2019-05-21 20:03:55 +00:00
xml encoding/xml: only initialize nil struct fields when decoding 2020-05-28 22:48:53 +00:00
encoding.go