Skip to content

Commit c064b3b

Browse files
committed
Fixed a corner case (bad UTF16 low surrogate) and more cleanup
1 parent 2f2fff5 commit c064b3b

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

parserescapes.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ func decodeUnicodeEscape(in []byte) (rune, int) {
5959
} else if r2, ok := decodeSingleUnicodeEscape(in[6:]); !ok { // Note: previous decodeSingleUnicodeEscape success guarantees at least 6 bytes remain
6060
// UTF16 "high surrogate" without manditory valid following Unicode escape for the "low surrogate"
6161
return utf8.RuneError, -1
62+
} else if r2 < lowSurrogateOffset {
63+
// Invalid UTF16 "low surrogate"
64+
return utf8.RuneError, -1
6265
} else {
6366
// Valid UTF16 surrogate pair
6467
return combineUTF16Surrogates(r, r2), 12

parserescapes_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ var multiUnicodeEscapeTests = append([]escapedUnicodeRuneTest{
5959
{in: `\uD800\uD`, isErr: true},
6060
{in: `\uD800\uDC`, isErr: true},
6161
{in: `\uD800\uDC0`, isErr: true},
62+
{in: `\uD800\uDBFF`, isErr: true}, // invalid low surrogate
6263
}, commonUnicodeEscapeTests...)
6364

6465
func TestDecodeSingleUnicodeEscape(t *testing.T) {
@@ -94,10 +95,10 @@ func TestDecodeUnicodeEscape(t *testing.T) {
9495
}
9596

9697
type unescapeTest struct {
97-
in string
98-
out string
99-
canAlloc bool
100-
isErr bool
98+
in string // escaped string
99+
out string // expected unescaped string
100+
canAlloc bool // can unescape cause an allocation (depending on buffer size)? true iff 'in' contains escape sequence(s)
101+
isErr bool // should this operation result in an error
101102
}
102103

103104
var unescapeTests = []unescapeTest{

0 commit comments

Comments
 (0)