@@ -158,49 +158,53 @@ func (d *structDecoder) tryOptimize() {
158
158
}
159
159
160
160
// decode from '\uXXXX'
161
- func decodeKeyCharByUnicodeRune (buf []byte , cursor int64 ) ([]byte , int64 ) {
161
+ func decodeKeyCharByUnicodeRune (buf []byte , cursor int64 ) ([]byte , int64 , error ) {
162
162
const defaultOffset = 4
163
163
const surrogateOffset = 6
164
164
165
+ if cursor + defaultOffset >= int64 (len (buf )) {
166
+ return nil , 0 , errors .ErrUnexpectedEndOfJSON ("escaped string" , cursor )
167
+ }
168
+
165
169
r := unicodeToRune (buf [cursor : cursor + defaultOffset ])
166
170
if utf16 .IsSurrogate (r ) {
167
171
cursor += defaultOffset
168
172
if cursor + surrogateOffset >= int64 (len (buf )) || buf [cursor ] != '\\' || buf [cursor + 1 ] != 'u' {
169
- return []byte (string (unicode .ReplacementChar )), cursor + defaultOffset - 1
173
+ return []byte (string (unicode .ReplacementChar )), cursor + defaultOffset - 1 , nil
170
174
}
171
175
cursor += 2
172
176
r2 := unicodeToRune (buf [cursor : cursor + defaultOffset ])
173
177
if r := utf16 .DecodeRune (r , r2 ); r != unicode .ReplacementChar {
174
- return []byte (string (r )), cursor + defaultOffset - 1
178
+ return []byte (string (r )), cursor + defaultOffset - 1 , nil
175
179
}
176
180
}
177
- return []byte (string (r )), cursor + defaultOffset - 1
181
+ return []byte (string (r )), cursor + defaultOffset - 1 , nil
178
182
}
179
183
180
- func decodeKeyCharByEscapedChar (buf []byte , cursor int64 ) ([]byte , int64 ) {
184
+ func decodeKeyCharByEscapedChar (buf []byte , cursor int64 ) ([]byte , int64 , error ) {
181
185
c := buf [cursor ]
182
186
cursor ++
183
187
switch c {
184
188
case '"' :
185
- return []byte {'"' }, cursor
189
+ return []byte {'"' }, cursor , nil
186
190
case '\\' :
187
- return []byte {'\\' }, cursor
191
+ return []byte {'\\' }, cursor , nil
188
192
case '/' :
189
- return []byte {'/' }, cursor
193
+ return []byte {'/' }, cursor , nil
190
194
case 'b' :
191
- return []byte {'\b' }, cursor
195
+ return []byte {'\b' }, cursor , nil
192
196
case 'f' :
193
- return []byte {'\f' }, cursor
197
+ return []byte {'\f' }, cursor , nil
194
198
case 'n' :
195
- return []byte {'\n' }, cursor
199
+ return []byte {'\n' }, cursor , nil
196
200
case 'r' :
197
- return []byte {'\r' }, cursor
201
+ return []byte {'\r' }, cursor , nil
198
202
case 't' :
199
- return []byte {'\t' }, cursor
203
+ return []byte {'\t' }, cursor , nil
200
204
case 'u' :
201
205
return decodeKeyCharByUnicodeRune (buf , cursor )
202
206
}
203
- return nil , cursor
207
+ return nil , cursor , nil
204
208
}
205
209
206
210
func decodeKeyByBitmapUint8 (d * structDecoder , buf []byte , cursor int64 ) (int64 , * structFieldSet , error ) {
@@ -242,7 +246,10 @@ func decodeKeyByBitmapUint8(d *structDecoder, buf []byte, cursor int64) (int64,
242
246
return 0 , nil , errors .ErrUnexpectedEndOfJSON ("string" , cursor )
243
247
case '\\' :
244
248
cursor ++
245
- chars , nextCursor := decodeKeyCharByEscapedChar (buf , cursor )
249
+ chars , nextCursor , err := decodeKeyCharByEscapedChar (buf , cursor )
250
+ if err != nil {
251
+ return 0 , nil , err
252
+ }
246
253
for _ , c := range chars {
247
254
curBit &= bitmap [keyIdx ][largeToSmallTable [c ]]
248
255
if curBit == 0 {
@@ -305,7 +312,10 @@ func decodeKeyByBitmapUint16(d *structDecoder, buf []byte, cursor int64) (int64,
305
312
return 0 , nil , errors .ErrUnexpectedEndOfJSON ("string" , cursor )
306
313
case '\\' :
307
314
cursor ++
308
- chars , nextCursor := decodeKeyCharByEscapedChar (buf , cursor )
315
+ chars , nextCursor , err := decodeKeyCharByEscapedChar (buf , cursor )
316
+ if err != nil {
317
+ return 0 , nil , err
318
+ }
309
319
for _ , c := range chars {
310
320
curBit &= bitmap [keyIdx ][largeToSmallTable [c ]]
311
321
if curBit == 0 {
0 commit comments