diff options
author | Tom Thorogood <me+google@tomthorogood.co.uk> | 2019-10-10 10:48:00 +0000 |
---|---|---|
committer | Daniel Martí <mvdan@mvdan.cc> | 2019-10-10 11:31:54 +0000 |
commit | 900ebcfe4d592486dd5bc50f5e8101ba65e32948 (patch) | |
tree | 86ca76dad4a1359cb0da98454f6f0c09b3be8d96 /src/encoding/json | |
parent | 3322f3e0ce6e8a8bbdd8e17803887a1f7119a52e (diff) | |
download | go-900ebcfe4d592486dd5bc50f5e8101ba65e32948.tar.gz go-900ebcfe4d592486dd5bc50f5e8101ba65e32948.zip |
encoding/json: stop escaping U+2028 and U+2029 in Compact
Compact has been inconsistently escaping only some problematic characters
(U+2028 and U+2029), but not others (<, > and &). This change addresses
this inconsistency by removing the escaping of U+2028 and U+2029.
Callers who need to escape the output of Compact should use HTMLEscape
which escapes <, >, &, U+2028 and U+2029.
Fixes #34070
Fixes #30357
Updates #5836
Change-Id: Icfce7691d2b8b1d9b05ba7b64d2d1e4f3b67871b
GitHub-Last-Rev: 38859fe3e2fd586bbd45175c2742f7b123836bf3
GitHub-Pull-Request: golang/go#34804
Reviewed-on: https://go-review.googlesource.com/c/go/+/200217
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/encoding/json')
-rw-r--r-- | src/encoding/json/indent.go | 2 | ||||
-rw-r--r-- | src/encoding/json/scanner_test.go | 5 |
2 files changed, 4 insertions, 3 deletions
diff --git a/src/encoding/json/indent.go b/src/encoding/json/indent.go index fba19548c9..06adfc1263 100644 --- a/src/encoding/json/indent.go +++ b/src/encoding/json/indent.go @@ -28,7 +28,7 @@ func compact(dst *bytes.Buffer, src []byte, escape bool) error { start = i + 1 } // Convert U+2028 and U+2029 (E2 80 A8 and E2 80 A9). - if c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xA8 { + if escape && c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xA8 { if start < i { dst.Write(src[start:i]) } diff --git a/src/encoding/json/scanner_test.go b/src/encoding/json/scanner_test.go index 6cdbe7d301..3737516a45 100644 --- a/src/encoding/json/scanner_test.go +++ b/src/encoding/json/scanner_test.go @@ -48,6 +48,7 @@ var examples = []example{ {`[1,2,3]`, "[\n\t1,\n\t2,\n\t3\n]"}, {`{"x":1}`, "{\n\t\"x\": 1\n}"}, {ex1, ex1i}, + {"{\"\":\"<>&\u2028\u2029\"}", "{\n\t\"\": \"<>&\u2028\u2029\"\n}"}, // See golang.org/issue/34070 } var ex1 = `[true,false,null,"x",1,1.5,0,-5e+2]` @@ -89,8 +90,8 @@ func TestCompactSeparators(t *testing.T) { tests := []struct { in, compact string }{ - {"{\"\u2028\": 1}", `{"\u2028":1}`}, - {"{\"\u2029\" :2}", `{"\u2029":2}`}, + {"{\"\u2028\": 1}", "{\"\u2028\":1}"}, + {"{\"\u2029\" :2}", "{\"\u2029\":2}"}, } for _, tt := range tests { var buf bytes.Buffer |