Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/json: marshal result of string type struct field with ",string" option change in go1.14 [1.14 backport] #38178

gopherbot opened this issue Mar 31, 2020 · 4 comments
CherryPickApproved Used during the release process for point releases FrozenDueToAge


Copy link

@mvdan requested issue #38173 to be considered for backport to the next 1.14 minor release.

@gopherbot, please open a backport issue for 1.14.

I think this is a regression - was a good change, but we didn't properly think of all the edge cases. I have a fix nearly ready. CC @breml @dsnet

@gopherbot gopherbot added the CherryPickCandidate Used during the release process for point releases label Mar 31, 2020
@gopherbot gopherbot added this to the Go1.14.2 milestone Mar 31, 2020
@andybons andybons modified the milestones: Go1.14.2, Go1.14.3 Apr 8, 2020
Copy link

mvdan commented Apr 16, 2020

The CL fixing this issue has been available since March - it's proving difficult to get the change properly approved during this time, so any help is appreciated. It would be a shame for it to get pushed back to 1.14.4 once again.

Copy link

dmitshur commented May 6, 2020

Approved as this is a Go 1.14-specific regression with no workaround.

@dmitshur dmitshur added CherryPickApproved Used during the release process for point releases and removed CherryPickCandidate Used during the release process for point releases labels May 6, 2020
Copy link

Change mentions this issue: [release-branch.go1.14] encoding/json: properly encode strings with ",string" again

@andybons andybons modified the milestones: Go1.14.3, Go1.14.4 May 14, 2020
Copy link

Closed by merging 8ca58ff to release-branch.go1.14.

gopherbot pushed a commit that referenced this issue May 27, 2020
…,string" again fixed one bug when one encodes a string with the
",string" option: if SetEscapeHTML(false) is used, we should not be
using HTML escaping for the inner string encoding. The CL correctly
fixed that.

The CL also tried to speed up this edge case. By avoiding an entire new
call to Marshal, the new Issue34127 benchmark reduced its time/op by
45%, and lowered the allocs/op from 3 to 2.

However, that last optimization wasn't correct:

	Since Go 1.2 every string can be marshaled to JSON without error
	even if it contains invalid UTF-8 byte sequences. Therefore
	there is no need to use Marshal again for the only reason of
	enclosing the string in double quotes.

JSON string encoding isn't just about adding quotes and taking care of
invalid UTF-8. We also need to escape some characters, like tabs and

The new code failed to do that. The bug resulted in the added test case
failing to roundtrip properly; before our fix here, we'd see an error:

	invalid use of ,string struct tag, trying to unmarshal "\"\b\f\n\r\t\"\\\"" into string

If you pay close attention, you'll notice that the special characters
like tab and newline are only encoded once, not twice. When decoding
with the ",string" option, the outer string decode works, but the inner
string decode fails, as we are now decoding a JSON string with unescaped
special characters.

The fix we apply here isn't to go back to Marshal, as that would
re-introduce the bug with SetEscapeHTML(false). Instead, we can use a
new encode state from the pool - it results in minimal performance
impact, and even reduces allocs/op further. The performance impact seems
fair, given that we need to check the entire string for characters that
need to be escaped.

	name          old time/op    new time/op    delta
	Issue34127-8    89.7ns ± 2%   100.8ns ± 1%  +12.27%  (p=0.000 n=8+8)

	name          old alloc/op   new alloc/op   delta
	Issue34127-8     40.0B ± 0%     32.0B ± 0%  -20.00%  (p=0.000 n=8+8)

	name          old allocs/op  new allocs/op  delta
	Issue34127-8      2.00 ± 0%      1.00 ± 0%  -50.00%  (p=0.000 n=8+8)

Instead of adding another standalone test, we convert an existing
"string tag" test to be table-based, and add another test case there.

One test case from the original CL also had to be amended, due to the
same problem - when escaping '<' due to SetEscapeHTML(true), we need to
end up with double escaping, since we're using ",string".

Fixes #38178.
For #38173.

Change-Id: I2b0df9e4f1d3452fff74fe910e189c930dde4b5b
Run-TryBot: Daniel Martí <>
TryBot-Result: Gobot Gobot <>
Reviewed-by: Joe Tsai <>
(cherry picked from commit b1a48af)
Reviewed-by: Daniel Martí <>
Run-TryBot: Dmitri Shuralyov <>
@golang golang locked and limited conversation to collaborators May 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
CherryPickApproved Used during the release process for point releases FrozenDueToAge
None yet

No branches or pull requests

4 participants