Skip to content

Commit 7b96513

Browse files
committed
Fixed stringEnd escape handling
1 parent 332abcf commit 7b96513

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

parser.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,18 @@ func nextValue(data []byte) int {
2323
// Tries to find the end of string
2424
// Support if string contains escaped quote symbols.
2525
func stringEnd(data []byte) int {
26-
i := 0
27-
28-
for len(data) > i {
29-
if data[i] != '"' {
30-
i++
31-
continue
32-
}
33-
34-
// If it just escaped \", continue
35-
if i >= 1 && data[i-1] == '\\' {
36-
i++
37-
continue
38-
} else {
39-
return i + 1
26+
escaped := false
27+
for i, c := range data {
28+
switch c {
29+
case '\\':
30+
escaped = !escaped
31+
case '"':
32+
if !escaped {
33+
return i + 1
34+
}
35+
escaped = false
36+
default:
37+
escaped = false
4038
}
4139
}
4240

parser_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ var getTests = []Test{
127127
isFound: true,
128128
data: `\\\"`,
129129
},
130+
Test{
131+
desc: `unescaped backslash quote`,
132+
json: `{"a": "\\"}`,
133+
path: []string{"a"},
134+
isFound: true,
135+
data: `\\`,
136+
},
130137
Test{
131138
desc: `unicode in JSON`,
132139
json: `{"a": "15°C"}`,

0 commit comments

Comments
 (0)