]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/json: stop escaping U+2028 and U+2029 in Compact
authorTom Thorogood <me+google@tomthorogood.co.uk>
Thu, 10 Oct 2019 10:48:00 +0000 (10:48 +0000)
committerDaniel Martí <mvdan@mvdan.cc>
Thu, 10 Oct 2019 11:31:54 +0000 (11:31 +0000)
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>

src/encoding/json/indent.go
src/encoding/json/scanner_test.go

index fba19548c92721d2f2fb0c63dd3accaa8a3077d3..06adfc12635aab10474ebc813f3d38a0a3b0a515 100644 (file)
@@ -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])
                        }
index 6cdbe7d301e880efa0b74afc813c246649803270..3737516a45065caf94388a183ad6ecb06d1658d0 100644 (file)
@@ -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