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

types/json: fix an over-quoted bug in BinaryJSON.Unquote function (#11934) #11955

Merged
merged 6 commits into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion expression/builtin_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 10 additions & 12 deletions types/json/binary_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
// 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
}
tlen := len(tmp)
if tlen < 2 {
return tmp, 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
}
Expand Down
1 change: 1 addition & 0 deletions types/json/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down