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

escape special characters in strings #1808

Merged
merged 1 commit into from
Jun 1, 2023
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
44 changes: 43 additions & 1 deletion sql/types/json_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ import (
"strconv"
)

var isEscaped = [256]bool{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could combine these two and use esc, ok := escapeSeq[r] checks, any reason not to?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it would be slower. I could be wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That requires a map instead of an array.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That requires a map instead of an array.

var escapeSeq = [256][]byte{}

func init() {
isEscaped[uint8('\b')] = true
isEscaped[uint8('\f')] = true
isEscaped[uint8('\n')] = true
isEscaped[uint8('\r')] = true
isEscaped[uint8('\t')] = true
isEscaped[uint8('"')] = true
isEscaped[uint8('\\')] = true

escapeSeq[uint8('\b')] = []byte("\\b")
escapeSeq[uint8('\f')] = []byte("\\f")
escapeSeq[uint8('\n')] = []byte("\\n")
escapeSeq[uint8('\r')] = []byte("\\r")
escapeSeq[uint8('\t')] = []byte("\\t")
escapeSeq[uint8('"')] = []byte("\\\"")
escapeSeq[uint8('\\')] = []byte("\\\\")
}

type NoCopyBuilder struct {
buffers [][]byte
curr int
Expand Down Expand Up @@ -191,7 +212,28 @@ func writeMarshalledValue(writer io.Writer, val interface{}) error {

case string:
writer.Write([]byte{'"'})
writer.Write([]byte(val))
// iterate over each rune in the string to escape any special characters
start := 0
for i, r := range val {
if r > '\\' {
continue
}

b := uint8(r)
if isEscaped[b] {
if start != i {
writer.Write([]byte(val[start:i]))
}

writer.Write(escapeSeq[b])
start = i + 1
}
}

if start != len(val) {
writer.Write([]byte(val[start:]))
}

writer.Write([]byte{'"'})
return nil

Expand Down
24 changes: 24 additions & 0 deletions sql/types/json_encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,30 @@ func TestMarshalToMySqlString(t *testing.T) {
},
expected: `{"baz": "qux", "foo": "bar"}`,
},
{
name: "string formatting",
val: []string{
`simple`,
`With "quotes"`,
`With "quotes" not at the end`,
`with 'single quotes'`,
`with
newlines
`,
`with \n escaped newline`,
`with `,
},
expected: `["simple", "With \"quotes\"", "With \"quotes\" not at the end", "with 'single quotes'", "with\nnewlines\n", "with \\n escaped newline", "with\t"]`,
},
{
name: "complicated string",
val: []string{
`{
"nested": "json",
"nested_escapedQuotes": "here \"you\" go"
}`},
expected: `["{\n\t\"nested\": \"json\",\n\t\"nested_escapedQuotes\": \"here \\\"you\\\" go\"\n}"]`,
},
}

for _, test := range tests {
Expand Down