Skip to content
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
2 changes: 1 addition & 1 deletion json.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func compactUnicodeEscape(input, output []byte, index int) ([]byte, int) {
// Otherwise the character only needs escaping if it is a QUOTE '"' or BACKSLASH '\\'.
output = append(output, '\\', byte(c))
} else if utf16.IsSurrogate(c) {
if input[index] != '\\' && input[index+1] != 'u' {
if input[index] != '\\' || input[index+1] != 'u' {
return output, index
}
index += 2 // skip the \u"
Expand Down
20 changes: 20 additions & 0 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,26 @@ func TestCompactUnicodeEscapeWithUTF16Surrogate(t *testing.T) {
}
}

func TestCompactUnicodeEscapeWithBadUTF16Surrogate(t *testing.T) {
input := []byte(`\ud83d\zdc08`)
output, n := compactUnicodeEscape(input[2:], nil, 0)
if n != 4 {
t.Fatalf("should have consumed 4 bytes but consumed %d bytes", n)
}
if string(output) != "" {
t.Fatalf("expected output to be empty")
}

input = []byte(`\ud83d udc08`)
output, n = compactUnicodeEscape(input[2:], nil, 0)
if n != 4 {
t.Fatalf("should have consumed 4 bytes but consumed %d bytes", n)
}
if string(output) != "" {
t.Fatalf("expected output to be empty")
}
}

func testReadHex(t *testing.T, input string, want rune) {
got := readHexDigits([]byte(input))
if want != got {
Expand Down