From e415ced76e5eb2f5b4d1790d4ea9007bbb8c7d42 Mon Sep 17 00:00:00 2001 From: bb7133 Date: Fri, 30 Aug 2019 17:18:35 +0800 Subject: [PATCH] types/json: fix an over-quoted bug in `BinaryJSON.Unquote` function (#11934) --- expression/builtin_json_test.go | 4 +++- types/json/binary_functions.go | 20 +++++++++----------- types/json/binary_test.go | 1 + 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/expression/builtin_json_test.go b/expression/builtin_json_test.go index 6b44ffdb0097b..8da86f1ec8c73 100644 --- a/expression/builtin_json_test.go +++ b/expression/builtin_json_test.go @@ -95,8 +95,10 @@ func (s *testEvaluatorSuite) TestJSONUnquote(c *C) { {`{"a": "b"}`, `{"a": "b"}`}, {`"hello,\"quoted string\",world"`, `hello,"quoted string",world`}, {`"hello,\"宽字符\",world"`, `hello,"宽字符",world`}, - {`Invalid Json string\tis OK`, `Invalid Json string is OK`}, + {`Invalid Json string\tis OK`, `Invalid Json string\tis OK`}, {`"1\\u2232\\u22322"`, `1\u2232\u22322`}, + {`"[{\"x\":\"{\\\"y\\\":12}\"}]"`, `[{"x":"{\"y\":12}"}]`}, + {`[{\"x\":\"{\\\"y\\\":12}\"}]`, `[{\"x\":\"{\\\"y\\\":12}\"}]`}, } dtbl := tblToDtbl(tbl) for _, t := range dtbl { diff --git a/types/json/binary_functions.go b/types/json/binary_functions.go index 18dc0d2eed1b3..4a0cfd50d82ba 100644 --- a/types/json/binary_functions.go +++ b/types/json/binary_functions.go @@ -66,19 +66,17 @@ func (bj BinaryJSON) Unquote() (string, error) { switch bj.TypeCode { case TypeCodeString: tmp := string(hack.String(bj.GetString())) - s, err := unquoteString(tmp) - if err != nil { - return "", errors.Trace(err) + tlen := len(tmp) + if tlen < 2 { + return tmp, nil } - // Remove prefix and suffix '"'. - slen := len(s) - if slen > 1 { - head, tail := s[0], s[slen-1] - if head == '"' && tail == '"' { - return s[1 : slen-1], nil - } + head, tail := tmp[0], tmp[tlen-1] + if head == '"' && tail == '"' { + // Remove prefix and suffix '"' before unquoting + return unquoteString(tmp[1 : tlen-1]) } - return s, nil + // if value is not double quoted, do nothing + return tmp, nil default: return bj.String(), nil } diff --git a/types/json/binary_test.go b/types/json/binary_test.go index e31a65fb0e23c..dc4e997002e81 100644 --- a/types/json/binary_test.go +++ b/types/json/binary_test.go @@ -114,6 +114,7 @@ func (s *testJSONSuite) TestBinaryJSONUnquote(c *C) { }{ {j: `3`, unquoted: "3"}, {j: `"3"`, unquoted: "3"}, + {j: `"[{\"x\":\"{\\\"y\\\":12}\"}]"`, unquoted: `[{"x":"{\"y\":12}"}]`}, {j: `"hello, \"escaped quotes\" world"`, unquoted: "hello, \"escaped quotes\" world"}, {j: "\"\\u4f60\"", unquoted: "你"}, {j: `true`, unquoted: "true"},