Merge pull request #140 from goccy/feature/fix-unmarshal-type-error

Fix decoder
This commit is contained in:
Masaaki Goshima 2021-02-18 19:48:08 +09:00 committed by GitHub
commit 58a761643a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 380 additions and 204 deletions

View File

@ -15,8 +15,8 @@ func (d Delim) String() string {
} }
type decoder interface { type decoder interface {
decode([]byte, int64, unsafe.Pointer) (int64, error) decode([]byte, int64, int64, unsafe.Pointer) (int64, error)
decodeStream(*stream, unsafe.Pointer) error decodeStream(*stream, int64, unsafe.Pointer) error
} }
type Decoder struct { type Decoder struct {
@ -29,7 +29,8 @@ var (
) )
const ( const (
nul = '\000' nul = '\000'
maxDecodeNestingDepth = 10000
) )
func unmarshal(data []byte, v interface{}) error { func unmarshal(data []byte, v interface{}) error {
@ -45,7 +46,7 @@ func unmarshal(data []byte, v interface{}) error {
if err != nil { if err != nil {
return err return err
} }
if _, err := dec.decode(src, 0, header.ptr); err != nil { if _, err := dec.decode(src, 0, 0, header.ptr); err != nil {
return err return err
} }
return nil return nil
@ -64,7 +65,7 @@ func unmarshalNoEscape(data []byte, v interface{}) error {
if err != nil { if err != nil {
return err return err
} }
if _, err := dec.decode(src, 0, noescape(header.ptr)); err != nil { if _, err := dec.decode(src, 0, 0, noescape(header.ptr)); err != nil {
return err return err
} }
return nil return nil
@ -78,7 +79,7 @@ func noescape(p unsafe.Pointer) unsafe.Pointer {
} }
func validateType(typ *rtype, p uintptr) error { func validateType(typ *rtype, p uintptr) error {
if typ.Kind() != reflect.Ptr || p == 0 { if typ == nil || typ.Kind() != reflect.Ptr || p == 0 {
return &InvalidUnmarshalError{Type: rtype2type(typ)} return &InvalidUnmarshalError{Type: rtype2type(typ)}
} }
return nil return nil
@ -147,7 +148,7 @@ func (d *Decoder) Decode(v interface{}) error {
return err return err
} }
s := d.s s := d.s
if err := dec.decodeStream(s, header.ptr); err != nil { if err := dec.decodeStream(s, 0, header.ptr); err != nil {
return err return err
} }
s.reset() s.reset()

View File

@ -18,18 +18,18 @@ func newAnonymousFieldDecoder(structType *rtype, offset uintptr, dec decoder) *a
} }
} }
func (d *anonymousFieldDecoder) decodeStream(s *stream, p unsafe.Pointer) error { func (d *anonymousFieldDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
if *(*unsafe.Pointer)(p) == nil { if *(*unsafe.Pointer)(p) == nil {
*(*unsafe.Pointer)(p) = unsafe_New(d.structType) *(*unsafe.Pointer)(p) = unsafe_New(d.structType)
} }
p = *(*unsafe.Pointer)(p) p = *(*unsafe.Pointer)(p)
return d.dec.decodeStream(s, unsafe.Pointer(uintptr(p)+d.offset)) return d.dec.decodeStream(s, depth, unsafe.Pointer(uintptr(p)+d.offset))
} }
func (d *anonymousFieldDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) { func (d *anonymousFieldDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
if *(*unsafe.Pointer)(p) == nil { if *(*unsafe.Pointer)(p) == nil {
*(*unsafe.Pointer)(p) = unsafe_New(d.structType) *(*unsafe.Pointer)(p) = unsafe_New(d.structType)
} }
p = *(*unsafe.Pointer)(p) p = *(*unsafe.Pointer)(p)
return d.dec.decode(buf, cursor, unsafe.Pointer(uintptr(p)+d.offset)) return d.dec.decode(buf, cursor, depth, unsafe.Pointer(uintptr(p)+d.offset))
} }

View File

@ -11,9 +11,11 @@ type arrayDecoder struct {
alen int alen int
structName string structName string
fieldName string fieldName string
zeroValue unsafe.Pointer
} }
func newArrayDecoder(dec decoder, elemType *rtype, alen int, structName, fieldName string) *arrayDecoder { func newArrayDecoder(dec decoder, elemType *rtype, alen int, structName, fieldName string) *arrayDecoder {
zeroValue := *(*unsafe.Pointer)(unsafe_New(elemType))
return &arrayDecoder{ return &arrayDecoder{
valueDecoder: dec, valueDecoder: dec,
elemType: elemType, elemType: elemType,
@ -21,10 +23,16 @@ func newArrayDecoder(dec decoder, elemType *rtype, alen int, structName, fieldNa
alen: alen, alen: alen,
structName: structName, structName: structName,
fieldName: fieldName, fieldName: fieldName,
zeroValue: zeroValue,
} }
} }
func (d *arrayDecoder) decodeStream(s *stream, p unsafe.Pointer) error { func (d *arrayDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
depth++
if depth > maxDecodeNestingDepth {
return errExceededMaxDepth(s.char(), s.cursor)
}
for { for {
switch s.char() { switch s.char() {
case ' ', '\n', '\t', '\r': case ' ', '\n', '\t', '\r':
@ -38,21 +46,26 @@ func (d *arrayDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
for { for {
s.cursor++ s.cursor++
if idx < d.alen { if idx < d.alen {
if err := d.valueDecoder.decodeStream(s, unsafe.Pointer(uintptr(p)+uintptr(idx)*d.size)); err != nil { if err := d.valueDecoder.decodeStream(s, depth, unsafe.Pointer(uintptr(p)+uintptr(idx)*d.size)); err != nil {
return err return err
} }
} else { } else {
if err := s.skipValue(); err != nil { if err := s.skipValue(depth); err != nil {
return err return err
} }
} }
idx++
s.skipWhiteSpace() s.skipWhiteSpace()
switch s.char() { switch s.char() {
case ']': case ']':
for idx < d.alen {
*(*unsafe.Pointer)(unsafe.Pointer(uintptr(p) + uintptr(idx)*d.size)) = d.zeroValue
idx++
}
s.cursor++ s.cursor++
return nil return nil
case ',': case ',':
idx++ continue
case nul: case nul:
if s.read() { if s.read() {
continue continue
@ -76,7 +89,12 @@ ERROR:
return errUnexpectedEndOfJSON("array", s.totalOffset()) return errUnexpectedEndOfJSON("array", s.totalOffset())
} }
func (d *arrayDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) { func (d *arrayDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
depth++
if depth > maxDecodeNestingDepth {
return 0, errExceededMaxDepth(buf[cursor], cursor)
}
buflen := int64(len(buf)) buflen := int64(len(buf))
for ; cursor < buflen; cursor++ { for ; cursor < buflen; cursor++ {
switch buf[cursor] { switch buf[cursor] {
@ -103,25 +121,29 @@ func (d *arrayDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64
for { for {
cursor++ cursor++
if idx < d.alen { if idx < d.alen {
c, err := d.valueDecoder.decode(buf, cursor, unsafe.Pointer(uintptr(p)+uintptr(idx)*d.size)) c, err := d.valueDecoder.decode(buf, cursor, depth, unsafe.Pointer(uintptr(p)+uintptr(idx)*d.size))
if err != nil { if err != nil {
return 0, err return 0, err
} }
cursor = c cursor = c
} else { } else {
c, err := skipValue(buf, cursor) c, err := skipValue(buf, cursor, depth)
if err != nil { if err != nil {
return 0, err return 0, err
} }
cursor = c cursor = c
} }
idx++
cursor = skipWhiteSpace(buf, cursor) cursor = skipWhiteSpace(buf, cursor)
switch buf[cursor] { switch buf[cursor] {
case ']': case ']':
for idx < d.alen {
*(*unsafe.Pointer)(unsafe.Pointer(uintptr(p) + uintptr(idx)*d.size)) = d.zeroValue
idx++
}
cursor++ cursor++
return cursor, nil return cursor, nil
case ',': case ',':
idx++
continue continue
default: default:
return 0, errInvalidCharacter(buf[cursor], "array", cursor) return 0, errInvalidCharacter(buf[cursor], "array", cursor)

View File

@ -61,7 +61,7 @@ func falseBytes(s *stream) error {
return nil return nil
} }
func (d *boolDecoder) decodeStream(s *stream, p unsafe.Pointer) error { func (d *boolDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
s.skipWhiteSpace() s.skipWhiteSpace()
for { for {
switch s.char() { switch s.char() {
@ -94,7 +94,7 @@ ERROR:
return errUnexpectedEndOfJSON("bool", s.totalOffset()) return errUnexpectedEndOfJSON("bool", s.totalOffset())
} }
func (d *boolDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) { func (d *boolDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
buflen := int64(len(buf)) buflen := int64(len(buf))
cursor = skipWhiteSpace(buf, cursor) cursor = skipWhiteSpace(buf, cursor)
switch buf[cursor] { switch buf[cursor] {

View File

@ -35,8 +35,8 @@ func newBytesDecoder(typ *rtype, structName string, fieldName string) *bytesDeco
} }
} }
func (d *bytesDecoder) decodeStream(s *stream, p unsafe.Pointer) error { func (d *bytesDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
bytes, err := d.decodeStreamBinary(s, p) bytes, err := d.decodeStreamBinary(s, depth, p)
if err != nil { if err != nil {
return err return err
} }
@ -54,8 +54,8 @@ func (d *bytesDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
return nil return nil
} }
func (d *bytesDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) { func (d *bytesDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
bytes, c, err := d.decodeBinary(buf, cursor, p) bytes, c, err := d.decodeBinary(buf, cursor, depth, p)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -94,7 +94,7 @@ ERROR:
return nil, errUnexpectedEndOfJSON("[]byte", s.totalOffset()) return nil, errUnexpectedEndOfJSON("[]byte", s.totalOffset())
} }
func (d *bytesDecoder) decodeStreamBinary(s *stream, p unsafe.Pointer) ([]byte, error) { func (d *bytesDecoder) decodeStreamBinary(s *stream, depth int64, p unsafe.Pointer) ([]byte, error) {
for { for {
switch s.char() { switch s.char() {
case ' ', '\n', '\t', '\r': case ' ', '\n', '\t', '\r':
@ -114,7 +114,7 @@ func (d *bytesDecoder) decodeStreamBinary(s *stream, p unsafe.Pointer) ([]byte,
Offset: s.totalOffset(), Offset: s.totalOffset(),
} }
} }
if err := d.sliceDecoder.decodeStream(s, p); err != nil { if err := d.sliceDecoder.decodeStream(s, depth, p); err != nil {
return nil, err return nil, err
} }
return nil, nil return nil, nil
@ -128,7 +128,7 @@ func (d *bytesDecoder) decodeStreamBinary(s *stream, p unsafe.Pointer) ([]byte,
return nil, errNotAtBeginningOfValue(s.totalOffset()) return nil, errNotAtBeginningOfValue(s.totalOffset())
} }
func (d *bytesDecoder) decodeBinary(buf []byte, cursor int64, p unsafe.Pointer) ([]byte, int64, error) { func (d *bytesDecoder) decodeBinary(buf []byte, cursor, depth int64, p unsafe.Pointer) ([]byte, int64, error) {
for { for {
switch buf[cursor] { switch buf[cursor] {
case ' ', '\n', '\t', '\r': case ' ', '\n', '\t', '\r':
@ -154,7 +154,7 @@ func (d *bytesDecoder) decodeBinary(buf []byte, cursor int64, p unsafe.Pointer)
Offset: cursor, Offset: cursor,
} }
} }
c, err := d.sliceDecoder.decode(buf, cursor, p) c, err := d.sliceDecoder.decode(buf, cursor, depth, p)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }

View File

@ -1,8 +1,10 @@
package json package json
import ( import (
"fmt"
"reflect" "reflect"
"strings" "strings"
"unicode"
"unsafe" "unsafe"
) )
@ -247,7 +249,7 @@ func decodeCompileInterface(typ *rtype, structName, fieldName string) (decoder,
return newInterfaceDecoder(typ, structName, fieldName), nil return newInterfaceDecoder(typ, structName, fieldName), nil
} }
func decodeRemoveConflictFields(fieldMap map[string]*structFieldSet, conflictedMap map[string]struct{}, dec *structDecoder, baseOffset uintptr) { func decodeRemoveConflictFields(fieldMap map[string]*structFieldSet, conflictedMap map[string]struct{}, dec *structDecoder, field reflect.StructField) {
for k, v := range dec.fieldMap { for k, v := range dec.fieldMap {
if _, exists := conflictedMap[k]; exists { if _, exists := conflictedMap[k]; exists {
// already conflicted key // already conflicted key
@ -257,7 +259,7 @@ func decodeRemoveConflictFields(fieldMap map[string]*structFieldSet, conflictedM
if !exists { if !exists {
fieldSet := &structFieldSet{ fieldSet := &structFieldSet{
dec: v.dec, dec: v.dec,
offset: baseOffset + v.offset, offset: field.Offset + v.offset,
isTaggedKey: v.isTaggedKey, isTaggedKey: v.isTaggedKey,
key: k, key: k,
keyLen: int64(len(k)), keyLen: int64(len(k)),
@ -281,7 +283,7 @@ func decodeRemoveConflictFields(fieldMap map[string]*structFieldSet, conflictedM
if v.isTaggedKey { if v.isTaggedKey {
fieldSet := &structFieldSet{ fieldSet := &structFieldSet{
dec: v.dec, dec: v.dec,
offset: baseOffset + v.offset, offset: field.Offset + v.offset,
isTaggedKey: v.isTaggedKey, isTaggedKey: v.isTaggedKey,
key: k, key: k,
keyLen: int64(len(k)), keyLen: int64(len(k)),
@ -318,6 +320,7 @@ func decodeCompileStruct(typ *rtype, structName, fieldName string, structTypeToD
if isIgnoredStructField(field) { if isIgnoredStructField(field) {
continue continue
} }
isUnexportedField := unicode.IsLower([]rune(field.Name)[0])
tag := structTagFromField(field) tag := structTagFromField(field)
dec, err := decodeCompile(type2rtype(field.Type), structName, field.Name, structTypeToDecoder) dec, err := decodeCompile(type2rtype(field.Type), structName, field.Name, structTypeToDecoder)
if err != nil { if err != nil {
@ -329,13 +332,20 @@ func decodeCompileStruct(typ *rtype, structName, fieldName string, structTypeToD
// recursive definition // recursive definition
continue continue
} }
decodeRemoveConflictFields(fieldMap, conflictedMap, stDec, field.Offset) decodeRemoveConflictFields(fieldMap, conflictedMap, stDec, field)
} else if pdec, ok := dec.(*ptrDecoder); ok { } else if pdec, ok := dec.(*ptrDecoder); ok {
contentDec := pdec.contentDecoder() contentDec := pdec.contentDecoder()
if pdec.typ == typ { if pdec.typ == typ {
// recursive definition // recursive definition
continue continue
} }
var fieldSetErr error
if isUnexportedField {
fieldSetErr = fmt.Errorf(
"json: cannot set embedded pointer to unexported struct: %v",
field.Type.Elem(),
)
}
if dec, ok := contentDec.(*structDecoder); ok { if dec, ok := contentDec.(*structDecoder); ok {
for k, v := range dec.fieldMap { for k, v := range dec.fieldMap {
if _, exists := conflictedMap[k]; exists { if _, exists := conflictedMap[k]; exists {
@ -350,6 +360,7 @@ func decodeCompileStruct(typ *rtype, structName, fieldName string, structTypeToD
isTaggedKey: v.isTaggedKey, isTaggedKey: v.isTaggedKey,
key: k, key: k,
keyLen: int64(len(k)), keyLen: int64(len(k)),
err: fieldSetErr,
} }
fieldMap[k] = fieldSet fieldMap[k] = fieldSet
lower := strings.ToLower(k) lower := strings.ToLower(k)
@ -374,6 +385,7 @@ func decodeCompileStruct(typ *rtype, structName, fieldName string, structTypeToD
isTaggedKey: v.isTaggedKey, isTaggedKey: v.isTaggedKey,
key: k, key: k,
keyLen: int64(len(k)), keyLen: int64(len(k)),
err: fieldSetErr,
} }
fieldMap[k] = fieldSet fieldMap[k] = fieldSet
lower := strings.ToLower(k) lower := strings.ToLower(k)

View File

@ -28,17 +28,29 @@ LOOP:
return cursor return cursor
} }
func skipObject(buf []byte, cursor int64) (int64, error) { func skipObject(buf []byte, cursor, depth int64) (int64, error) {
braceCount := 1 braceCount := 1
for { for {
switch buf[cursor] { switch buf[cursor] {
case '{': case '{':
braceCount++ braceCount++
depth++
if depth > maxDecodeNestingDepth {
return 0, errExceededMaxDepth(buf[cursor], cursor)
}
case '}': case '}':
depth--
braceCount-- braceCount--
if braceCount == 0 { if braceCount == 0 {
return cursor + 1, nil return cursor + 1, nil
} }
case '[':
depth++
if depth > maxDecodeNestingDepth {
return 0, errExceededMaxDepth(buf[cursor], cursor)
}
case ']':
depth--
case '"': case '"':
for { for {
cursor++ cursor++
@ -60,17 +72,29 @@ func skipObject(buf []byte, cursor int64) (int64, error) {
} }
} }
func skipArray(buf []byte, cursor int64) (int64, error) { func skipArray(buf []byte, cursor, depth int64) (int64, error) {
bracketCount := 1 bracketCount := 1
for { for {
switch buf[cursor] { switch buf[cursor] {
case '[': case '[':
bracketCount++ bracketCount++
depth++
if depth > maxDecodeNestingDepth {
return 0, errExceededMaxDepth(buf[cursor], cursor)
}
case ']': case ']':
bracketCount-- bracketCount--
depth--
if bracketCount == 0 { if bracketCount == 0 {
return cursor + 1, nil return cursor + 1, nil
} }
case '{':
depth++
if depth > maxDecodeNestingDepth {
return 0, errExceededMaxDepth(buf[cursor], cursor)
}
case '}':
depth--
case '"': case '"':
for { for {
cursor++ cursor++
@ -92,16 +116,16 @@ func skipArray(buf []byte, cursor int64) (int64, error) {
} }
} }
func skipValue(buf []byte, cursor int64) (int64, error) { func skipValue(buf []byte, cursor, depth int64) (int64, error) {
for { for {
switch buf[cursor] { switch buf[cursor] {
case ' ', '\t', '\n', '\r': case ' ', '\t', '\n', '\r':
cursor++ cursor++
continue continue
case '{': case '{':
return skipObject(buf, cursor+1) return skipObject(buf, cursor+1, depth+1)
case '[': case '[':
return skipArray(buf, cursor+1) return skipArray(buf, cursor+1, depth+1)
case '"': case '"':
for { for {
cursor++ cursor++

View File

@ -129,7 +129,7 @@ func (d *floatDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, erro
return nil, 0, errUnexpectedEndOfJSON("float", cursor) return nil, 0, errUnexpectedEndOfJSON("float", cursor)
} }
func (d *floatDecoder) decodeStream(s *stream, p unsafe.Pointer) error { func (d *floatDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
bytes, err := d.decodeStreamByte(s) bytes, err := d.decodeStreamByte(s)
if err != nil { if err != nil {
return err return err
@ -146,7 +146,7 @@ func (d *floatDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
return nil return nil
} }
func (d *floatDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) { func (d *floatDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
bytes, c, err := d.decodeByte(buf, cursor) bytes, c, err := d.decodeByte(buf, cursor)
if err != nil { if err != nil {
return 0, err return 0, err

View File

@ -173,7 +173,7 @@ func (d *intDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error)
} }
} }
func (d *intDecoder) decodeStream(s *stream, p unsafe.Pointer) error { func (d *intDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
bytes, err := d.decodeStreamByte(s) bytes, err := d.decodeStreamByte(s)
if err != nil { if err != nil {
return err return err
@ -201,7 +201,7 @@ func (d *intDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
return nil return nil
} }
func (d *intDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) { func (d *intDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
bytes, c, err := d.decodeByte(buf, cursor) bytes, c, err := d.decodeByte(buf, cursor)
if err != nil { if err != nil {
return 0, err return 0, err

View File

@ -42,9 +42,9 @@ var (
) )
) )
func decodeStreamUnmarshaler(s *stream, unmarshaler Unmarshaler) error { func decodeStreamUnmarshaler(s *stream, depth int64, unmarshaler Unmarshaler) error {
start := s.cursor start := s.cursor
if err := s.skipValue(); err != nil { if err := s.skipValue(depth); err != nil {
return err return err
} }
src := s.buf[start:s.cursor] src := s.buf[start:s.cursor]
@ -57,10 +57,10 @@ func decodeStreamUnmarshaler(s *stream, unmarshaler Unmarshaler) error {
return nil return nil
} }
func decodeUnmarshaler(buf []byte, cursor int64, unmarshaler Unmarshaler) (int64, error) { func decodeUnmarshaler(buf []byte, cursor, depth int64, unmarshaler Unmarshaler) (int64, error) {
cursor = skipWhiteSpace(buf, cursor) cursor = skipWhiteSpace(buf, cursor)
start := cursor start := cursor
end, err := skipValue(buf, cursor) end, err := skipValue(buf, cursor, depth)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -74,9 +74,9 @@ func decodeUnmarshaler(buf []byte, cursor int64, unmarshaler Unmarshaler) (int64
return end, nil return end, nil
} }
func decodeStreamTextUnmarshaler(s *stream, unmarshaler encoding.TextUnmarshaler, p unsafe.Pointer) error { func decodeStreamTextUnmarshaler(s *stream, depth int64, unmarshaler encoding.TextUnmarshaler, p unsafe.Pointer) error {
start := s.cursor start := s.cursor
if err := s.skipValue(); err != nil { if err := s.skipValue(depth); err != nil {
return err return err
} }
src := s.buf[start:s.cursor] src := s.buf[start:s.cursor]
@ -94,10 +94,10 @@ func decodeStreamTextUnmarshaler(s *stream, unmarshaler encoding.TextUnmarshaler
return nil return nil
} }
func decodeTextUnmarshaler(buf []byte, cursor int64, unmarshaler encoding.TextUnmarshaler, p unsafe.Pointer) (int64, error) { func decodeTextUnmarshaler(buf []byte, cursor, depth int64, unmarshaler encoding.TextUnmarshaler, p unsafe.Pointer) (int64, error) {
cursor = skipWhiteSpace(buf, cursor) cursor = skipWhiteSpace(buf, cursor)
start := cursor start := cursor
end, err := skipValue(buf, cursor) end, err := skipValue(buf, cursor, depth)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -115,7 +115,7 @@ func decodeTextUnmarshaler(buf []byte, cursor int64, unmarshaler encoding.TextUn
return end, nil return end, nil
} }
func (d *interfaceDecoder) decodeStreamEmptyInterface(s *stream, p unsafe.Pointer) error { func (d *interfaceDecoder) decodeStreamEmptyInterface(s *stream, depth int64, p unsafe.Pointer) error {
s.skipWhiteSpace() s.skipWhiteSpace()
for { for {
switch s.char() { switch s.char() {
@ -130,7 +130,7 @@ func (d *interfaceDecoder) decodeStreamEmptyInterface(s *stream, p unsafe.Pointe
newInterfaceDecoder(emptyInterfaceType, d.structName, d.fieldName), newInterfaceDecoder(emptyInterfaceType, d.structName, d.fieldName),
d.structName, d.structName,
d.fieldName, d.fieldName,
).decodeStream(s, ptr); err != nil { ).decodeStream(s, depth, ptr); err != nil {
return err return err
} }
*(*interface{})(p) = v *(*interface{})(p) = v
@ -144,13 +144,13 @@ func (d *interfaceDecoder) decodeStreamEmptyInterface(s *stream, p unsafe.Pointe
emptyInterfaceType.Size(), emptyInterfaceType.Size(),
d.structName, d.structName,
d.fieldName, d.fieldName,
).decodeStream(s, ptr); err != nil { ).decodeStream(s, depth, ptr); err != nil {
return err return err
} }
*(*interface{})(p) = v *(*interface{})(p) = v
return nil return nil
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return d.numDecoder(s).decodeStream(s, p) return d.numDecoder(s).decodeStream(s, depth, p)
case '"': case '"':
s.cursor++ s.cursor++
start := s.cursor start := s.cursor
@ -201,7 +201,7 @@ func (d *interfaceDecoder) decodeStreamEmptyInterface(s *stream, p unsafe.Pointe
return errNotAtBeginningOfValue(s.totalOffset()) return errNotAtBeginningOfValue(s.totalOffset())
} }
func (d *interfaceDecoder) decodeStream(s *stream, p unsafe.Pointer) error { func (d *interfaceDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
runtimeInterfaceValue := *(*interface{})(unsafe.Pointer(&interfaceHeader{ runtimeInterfaceValue := *(*interface{})(unsafe.Pointer(&interfaceHeader{
typ: d.typ, typ: d.typ,
ptr: p, ptr: p,
@ -209,22 +209,22 @@ func (d *interfaceDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
rv := reflect.ValueOf(runtimeInterfaceValue) rv := reflect.ValueOf(runtimeInterfaceValue)
if rv.NumMethod() > 0 && rv.CanInterface() { if rv.NumMethod() > 0 && rv.CanInterface() {
if u, ok := rv.Interface().(Unmarshaler); ok { if u, ok := rv.Interface().(Unmarshaler); ok {
return decodeStreamUnmarshaler(s, u) return decodeStreamUnmarshaler(s, depth, u)
} }
if u, ok := rv.Interface().(encoding.TextUnmarshaler); ok { if u, ok := rv.Interface().(encoding.TextUnmarshaler); ok {
return decodeStreamTextUnmarshaler(s, u, p) return decodeStreamTextUnmarshaler(s, depth, u, p)
} }
return &UnsupportedTypeError{Type: rv.Type()} return d.errUnmarshalType(rv.Type(), s.totalOffset())
} }
iface := rv.Interface() iface := rv.Interface()
ifaceHeader := (*interfaceHeader)(unsafe.Pointer(&iface)) ifaceHeader := (*interfaceHeader)(unsafe.Pointer(&iface))
typ := ifaceHeader.typ typ := ifaceHeader.typ
if ifaceHeader.ptr == nil || d.typ == typ || typ == nil { if ifaceHeader.ptr == nil || d.typ == typ || typ == nil {
// concrete type is empty interface // concrete type is empty interface
return d.decodeStreamEmptyInterface(s, p) return d.decodeStreamEmptyInterface(s, depth, p)
} }
if typ.Kind() == reflect.Ptr && typ.Elem() == d.typ || typ.Kind() != reflect.Ptr { if typ.Kind() == reflect.Ptr && typ.Elem() == d.typ || typ.Kind() != reflect.Ptr {
return d.decodeStreamEmptyInterface(s, p) return d.decodeStreamEmptyInterface(s, depth, p)
} }
s.skipWhiteSpace() s.skipWhiteSpace()
if s.char() == 'n' { if s.char() == 'n' {
@ -238,10 +238,20 @@ func (d *interfaceDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
if err != nil { if err != nil {
return err return err
} }
return decoder.decodeStream(s, ifaceHeader.ptr) return decoder.decodeStream(s, depth, ifaceHeader.ptr)
} }
func (d *interfaceDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) { func (d *interfaceDecoder) errUnmarshalType(typ reflect.Type, offset int64) *UnmarshalTypeError {
return &UnmarshalTypeError{
Value: typ.String(),
Type: typ,
Offset: offset,
Struct: d.structName,
Field: d.fieldName,
}
}
func (d *interfaceDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
runtimeInterfaceValue := *(*interface{})(unsafe.Pointer(&interfaceHeader{ runtimeInterfaceValue := *(*interface{})(unsafe.Pointer(&interfaceHeader{
typ: d.typ, typ: d.typ,
ptr: p, ptr: p,
@ -249,12 +259,12 @@ func (d *interfaceDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (i
rv := reflect.ValueOf(runtimeInterfaceValue) rv := reflect.ValueOf(runtimeInterfaceValue)
if rv.NumMethod() > 0 && rv.CanInterface() { if rv.NumMethod() > 0 && rv.CanInterface() {
if u, ok := rv.Interface().(Unmarshaler); ok { if u, ok := rv.Interface().(Unmarshaler); ok {
return decodeUnmarshaler(buf, cursor, u) return decodeUnmarshaler(buf, cursor, depth, u)
} }
if u, ok := rv.Interface().(encoding.TextUnmarshaler); ok { if u, ok := rv.Interface().(encoding.TextUnmarshaler); ok {
return decodeTextUnmarshaler(buf, cursor, u, p) return decodeTextUnmarshaler(buf, cursor, depth, u, p)
} }
return 0, &UnsupportedTypeError{Type: rv.Type()} return 0, d.errUnmarshalType(rv.Type(), cursor)
} }
iface := rv.Interface() iface := rv.Interface()
@ -262,10 +272,10 @@ func (d *interfaceDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (i
typ := ifaceHeader.typ typ := ifaceHeader.typ
if ifaceHeader.ptr == nil || d.typ == typ || typ == nil { if ifaceHeader.ptr == nil || d.typ == typ || typ == nil {
// concrete type is empty interface // concrete type is empty interface
return d.decodeEmptyInterface(buf, cursor, p) return d.decodeEmptyInterface(buf, cursor, depth, p)
} }
if typ.Kind() == reflect.Ptr && typ.Elem() == d.typ || typ.Kind() != reflect.Ptr { if typ.Kind() == reflect.Ptr && typ.Elem() == d.typ || typ.Kind() != reflect.Ptr {
return d.decodeEmptyInterface(buf, cursor, p) return d.decodeEmptyInterface(buf, cursor, depth, p)
} }
cursor = skipWhiteSpace(buf, cursor) cursor = skipWhiteSpace(buf, cursor)
if buf[cursor] == 'n' { if buf[cursor] == 'n' {
@ -289,10 +299,10 @@ func (d *interfaceDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (i
if err != nil { if err != nil {
return 0, err return 0, err
} }
return decoder.decode(buf, cursor, ifaceHeader.ptr) return decoder.decode(buf, cursor, depth, ifaceHeader.ptr)
} }
func (d *interfaceDecoder) decodeEmptyInterface(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) { func (d *interfaceDecoder) decodeEmptyInterface(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
cursor = skipWhiteSpace(buf, cursor) cursor = skipWhiteSpace(buf, cursor)
switch buf[cursor] { switch buf[cursor] {
case '{': case '{':
@ -306,7 +316,7 @@ func (d *interfaceDecoder) decodeEmptyInterface(buf []byte, cursor int64, p unsa
newInterfaceDecoder(emptyInterfaceType, d.structName, d.fieldName), newInterfaceDecoder(emptyInterfaceType, d.structName, d.fieldName),
d.structName, d.fieldName, d.structName, d.fieldName,
) )
cursor, err := dec.decode(buf, cursor, ptr) cursor, err := dec.decode(buf, cursor, depth, ptr)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -321,7 +331,7 @@ func (d *interfaceDecoder) decodeEmptyInterface(buf []byte, cursor int64, p unsa
emptyInterfaceType.Size(), emptyInterfaceType.Size(),
d.structName, d.fieldName, d.structName, d.fieldName,
) )
cursor, err := dec.decode(buf, cursor, ptr) cursor, err := dec.decode(buf, cursor, depth, ptr)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -330,12 +340,12 @@ func (d *interfaceDecoder) decodeEmptyInterface(buf []byte, cursor int64, p unsa
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return newFloatDecoder(d.structName, d.fieldName, func(p unsafe.Pointer, v float64) { return newFloatDecoder(d.structName, d.fieldName, func(p unsafe.Pointer, v float64) {
*(*interface{})(p) = v *(*interface{})(p) = v
}).decode(buf, cursor, p) }).decode(buf, cursor, depth, p)
case '"': case '"':
var v string var v string
ptr := unsafe.Pointer(&v) ptr := unsafe.Pointer(&v)
dec := newStringDecoder(d.structName, d.fieldName) dec := newStringDecoder(d.structName, d.fieldName)
cursor, err := dec.decode(buf, cursor, ptr) cursor, err := dec.decode(buf, cursor, depth, ptr)
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@ -33,7 +33,12 @@ func makemap(*rtype, int) unsafe.Pointer
//go:noescape //go:noescape
func mapassign(t *rtype, m unsafe.Pointer, key, val unsafe.Pointer) func mapassign(t *rtype, m unsafe.Pointer, key, val unsafe.Pointer)
func (d *mapDecoder) decodeStream(s *stream, p unsafe.Pointer) error { func (d *mapDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
depth++
if depth > maxDecodeNestingDepth {
return errExceededMaxDepth(s.char(), s.cursor)
}
s.skipWhiteSpace() s.skipWhiteSpace()
switch s.char() { switch s.char() {
case 'n': case 'n':
@ -47,7 +52,10 @@ func (d *mapDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
return errExpected("{ character for map value", s.totalOffset()) return errExpected("{ character for map value", s.totalOffset())
} }
s.skipWhiteSpace() s.skipWhiteSpace()
mapValue := makemap(d.mapType, 0) mapValue := *(*unsafe.Pointer)(p)
if mapValue == nil {
mapValue = makemap(d.mapType, 0)
}
if s.buf[s.cursor+1] == '}' { if s.buf[s.cursor+1] == '}' {
*(*unsafe.Pointer)(p) = mapValue *(*unsafe.Pointer)(p) = mapValue
s.cursor += 2 s.cursor += 2
@ -56,7 +64,7 @@ func (d *mapDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
for { for {
s.cursor++ s.cursor++
k := unsafe_New(d.keyType) k := unsafe_New(d.keyType)
if err := d.keyDecoder.decodeStream(s, k); err != nil { if err := d.keyDecoder.decodeStream(s, depth, k); err != nil {
return err return err
} }
s.skipWhiteSpace() s.skipWhiteSpace()
@ -68,7 +76,7 @@ func (d *mapDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
} }
s.cursor++ s.cursor++
v := unsafe_New(d.valueType) v := unsafe_New(d.valueType)
if err := d.valueDecoder.decodeStream(s, v); err != nil { if err := d.valueDecoder.decodeStream(s, depth, v); err != nil {
return err return err
} }
mapassign(d.mapType, mapValue, k, v) mapassign(d.mapType, mapValue, k, v)
@ -87,7 +95,12 @@ func (d *mapDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
} }
} }
func (d *mapDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) { func (d *mapDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
depth++
if depth > maxDecodeNestingDepth {
return 0, errExceededMaxDepth(buf[cursor], cursor)
}
cursor = skipWhiteSpace(buf, cursor) cursor = skipWhiteSpace(buf, cursor)
buflen := int64(len(buf)) buflen := int64(len(buf))
if buflen < 2 { if buflen < 2 {
@ -116,7 +129,10 @@ func (d *mapDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64,
} }
cursor++ cursor++
cursor = skipWhiteSpace(buf, cursor) cursor = skipWhiteSpace(buf, cursor)
mapValue := makemap(d.mapType, 0) mapValue := *(*unsafe.Pointer)(p)
if mapValue == nil {
mapValue = makemap(d.mapType, 0)
}
if buf[cursor] == '}' { if buf[cursor] == '}' {
**(**unsafe.Pointer)(unsafe.Pointer(&p)) = mapValue **(**unsafe.Pointer)(unsafe.Pointer(&p)) = mapValue
cursor++ cursor++
@ -124,7 +140,7 @@ func (d *mapDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64,
} }
for { for {
k := unsafe_New(d.keyType) k := unsafe_New(d.keyType)
keyCursor, err := d.keyDecoder.decode(buf, cursor, k) keyCursor, err := d.keyDecoder.decode(buf, cursor, depth, k)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -134,7 +150,7 @@ func (d *mapDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64,
} }
cursor++ cursor++
v := unsafe_New(d.valueType) v := unsafe_New(d.valueType)
valueCursor, err := d.valueDecoder.decode(buf, cursor, v) valueCursor, err := d.valueDecoder.decode(buf, cursor, depth, v)
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@ -20,7 +20,7 @@ func newNumberDecoder(structName, fieldName string, op func(unsafe.Pointer, Numb
} }
} }
func (d *numberDecoder) decodeStream(s *stream, p unsafe.Pointer) error { func (d *numberDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
bytes, err := d.floatDecoder.decodeStreamByte(s) bytes, err := d.floatDecoder.decodeStreamByte(s)
if err != nil { if err != nil {
return err return err
@ -30,7 +30,7 @@ func (d *numberDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
return nil return nil
} }
func (d *numberDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) { func (d *numberDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
bytes, c, err := d.floatDecoder.decodeByte(buf, cursor) bytes, c, err := d.floatDecoder.decodeByte(buf, cursor)
if err != nil { if err != nil {
return 0, err return 0, err

View File

@ -32,7 +32,7 @@ func (d *ptrDecoder) contentDecoder() decoder {
//go:linkname unsafe_New reflect.unsafe_New //go:linkname unsafe_New reflect.unsafe_New
func unsafe_New(*rtype) unsafe.Pointer func unsafe_New(*rtype) unsafe.Pointer
func (d *ptrDecoder) decodeStream(s *stream, p unsafe.Pointer) error { func (d *ptrDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
s.skipWhiteSpace() s.skipWhiteSpace()
if s.char() == nul { if s.char() == nul {
s.read() s.read()
@ -51,13 +51,13 @@ func (d *ptrDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
} else { } else {
newptr = *(*unsafe.Pointer)(p) newptr = *(*unsafe.Pointer)(p)
} }
if err := d.dec.decodeStream(s, newptr); err != nil { if err := d.dec.decodeStream(s, depth, newptr); err != nil {
return err return err
} }
return nil return nil
} }
func (d *ptrDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) { func (d *ptrDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
cursor = skipWhiteSpace(buf, cursor) cursor = skipWhiteSpace(buf, cursor)
if buf[cursor] == 'n' { if buf[cursor] == 'n' {
buflen := int64(len(buf)) buflen := int64(len(buf))
@ -86,7 +86,7 @@ func (d *ptrDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64,
} else { } else {
newptr = *(*unsafe.Pointer)(p) newptr = *(*unsafe.Pointer)(p)
} }
c, err := d.dec.decode(buf, cursor, newptr) c, err := d.dec.decode(buf, cursor, depth, newptr)
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@ -73,7 +73,12 @@ func (d *sliceDecoder) errNumber(offset int64) *UnmarshalTypeError {
} }
} }
func (d *sliceDecoder) decodeStream(s *stream, p unsafe.Pointer) error { func (d *sliceDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
depth++
if depth > maxDecodeNestingDepth {
return errExceededMaxDepth(s.char(), s.cursor)
}
for { for {
switch s.char() { switch s.char() {
case ' ', '\n', '\t', '\r': case ' ', '\n', '\t', '\r':
@ -109,7 +114,7 @@ func (d *sliceDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
dst := sliceHeader{data: data, len: idx, cap: capacity} dst := sliceHeader{data: data, len: idx, cap: capacity}
copySlice(d.elemType, dst, src) copySlice(d.elemType, dst, src)
} }
if err := d.valueDecoder.decodeStream(s, unsafe.Pointer(uintptr(data)+uintptr(idx)*d.size)); err != nil { if err := d.valueDecoder.decodeStream(s, depth, unsafe.Pointer(uintptr(data)+uintptr(idx)*d.size)); err != nil {
return err return err
} }
s.skipWhiteSpace() s.skipWhiteSpace()
@ -167,7 +172,12 @@ ERROR:
return errUnexpectedEndOfJSON("slice", s.totalOffset()) return errUnexpectedEndOfJSON("slice", s.totalOffset())
} }
func (d *sliceDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) { func (d *sliceDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
depth++
if depth > maxDecodeNestingDepth {
return 0, errExceededMaxDepth(buf[cursor], cursor)
}
buflen := int64(len(buf)) buflen := int64(len(buf))
for ; cursor < buflen; cursor++ { for ; cursor < buflen; cursor++ {
switch buf[cursor] { switch buf[cursor] {
@ -214,7 +224,7 @@ func (d *sliceDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64
dst := sliceHeader{data: data, len: idx, cap: capacity} dst := sliceHeader{data: data, len: idx, cap: capacity}
copySlice(d.elemType, dst, src) copySlice(d.elemType, dst, src)
} }
c, err := d.valueDecoder.decode(buf, cursor, unsafe.Pointer(uintptr(data)+uintptr(idx)*d.size)) c, err := d.valueDecoder.decode(buf, cursor, depth, unsafe.Pointer(uintptr(data)+uintptr(idx)*d.size))
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@ -97,19 +97,31 @@ LOOP:
} }
} }
func (s *stream) skipObject() error { func (s *stream) skipObject(depth int64) error {
braceCount := 1 braceCount := 1
_, cursor, p := s.stat() _, cursor, p := s.stat()
for { for {
switch char(p, cursor) { switch char(p, cursor) {
case '{': case '{':
braceCount++ braceCount++
depth++
if depth > maxDecodeNestingDepth {
return errExceededMaxDepth(s.char(), s.cursor)
}
case '}': case '}':
braceCount-- braceCount--
depth--
if braceCount == 0 { if braceCount == 0 {
s.cursor = cursor + 1 s.cursor = cursor + 1
return nil return nil
} }
case '[':
depth++
if depth > maxDecodeNestingDepth {
return errExceededMaxDepth(s.char(), s.cursor)
}
case ']':
depth--
case '"': case '"':
for { for {
cursor++ cursor++
@ -142,19 +154,31 @@ func (s *stream) skipObject() error {
} }
} }
func (s *stream) skipArray() error { func (s *stream) skipArray(depth int64) error {
bracketCount := 1 bracketCount := 1
_, cursor, p := s.stat() _, cursor, p := s.stat()
for { for {
switch char(p, cursor) { switch char(p, cursor) {
case '[': case '[':
bracketCount++ bracketCount++
depth++
if depth > maxDecodeNestingDepth {
return errExceededMaxDepth(s.char(), s.cursor)
}
case ']': case ']':
bracketCount-- bracketCount--
depth--
if bracketCount == 0 { if bracketCount == 0 {
s.cursor = cursor + 1 s.cursor = cursor + 1
return nil return nil
} }
case '{':
depth++
if depth > maxDecodeNestingDepth {
return errExceededMaxDepth(s.char(), s.cursor)
}
case '}':
depth--
case '"': case '"':
for { for {
cursor++ cursor++
@ -187,7 +211,7 @@ func (s *stream) skipArray() error {
} }
} }
func (s *stream) skipValue() error { func (s *stream) skipValue(depth int64) error {
_, cursor, p := s.stat() _, cursor, p := s.stat()
for { for {
switch char(p, cursor) { switch char(p, cursor) {
@ -203,10 +227,10 @@ func (s *stream) skipValue() error {
return errUnexpectedEndOfJSON("value of object", s.totalOffset()) return errUnexpectedEndOfJSON("value of object", s.totalOffset())
case '{': case '{':
s.cursor = cursor + 1 s.cursor = cursor + 1
return s.skipObject() return s.skipObject(depth + 1)
case '[': case '[':
s.cursor = cursor + 1 s.cursor = cursor + 1
return s.skipArray() return s.skipArray(depth + 1)
case '"': case '"':
for { for {
cursor++ cursor++

View File

@ -30,7 +30,7 @@ func (d *stringDecoder) errUnmarshalType(typeName string, offset int64) *Unmarsh
} }
} }
func (d *stringDecoder) decodeStream(s *stream, p unsafe.Pointer) error { func (d *stringDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
bytes, err := d.decodeStreamByte(s) bytes, err := d.decodeStreamByte(s)
if err != nil { if err != nil {
return err return err
@ -43,7 +43,7 @@ func (d *stringDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
return nil return nil
} }
func (d *stringDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) { func (d *stringDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
bytes, c, err := d.decodeByte(buf, cursor) bytes, c, err := d.decodeByte(buf, cursor)
if err != nil { if err != nil {
return 0, err return 0, err
@ -231,6 +231,8 @@ func (d *stringDecoder) decodeStreamByte(s *stream) ([]byte, error) {
continue continue
case '[': case '[':
return nil, d.errUnmarshalType("array", s.totalOffset()) return nil, d.errUnmarshalType("array", s.totalOffset())
case '{':
return nil, d.errUnmarshalType("object", s.totalOffset())
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return nil, d.errUnmarshalType("number", s.totalOffset()) return nil, d.errUnmarshalType("number", s.totalOffset())
case '"': case '"':
@ -257,6 +259,8 @@ func (d *stringDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, err
cursor++ cursor++
case '[': case '[':
return nil, 0, d.errUnmarshalType("array", cursor) return nil, 0, d.errUnmarshalType("array", cursor)
case '{':
return nil, 0, d.errUnmarshalType("object", cursor)
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return nil, 0, d.errUnmarshalType("number", cursor) return nil, 0, d.errUnmarshalType("number", cursor)
case '"': case '"':

View File

@ -15,6 +15,7 @@ type structFieldSet struct {
isTaggedKey bool isTaggedKey bool
key string key string
keyLen int64 keyLen int64
err error
} }
type structDecoder struct { type structDecoder struct {
@ -486,7 +487,12 @@ func decodeKeyStream(d *structDecoder, s *stream) (*structFieldSet, string, erro
return d.fieldMap[k], k, nil return d.fieldMap[k], k, nil
} }
func (d *structDecoder) decodeStream(s *stream, p unsafe.Pointer) error { func (d *structDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
depth++
if depth > maxDecodeNestingDepth {
return errExceededMaxDepth(s.char(), s.cursor)
}
s.skipWhiteSpace() s.skipWhiteSpace()
switch s.char() { switch s.char() {
case 'n': case 'n':
@ -524,13 +530,16 @@ func (d *structDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
} }
} }
if field != nil { if field != nil {
if err := field.dec.decodeStream(s, unsafe.Pointer(uintptr(p)+field.offset)); err != nil { if field.err != nil {
return field.err
}
if err := field.dec.decodeStream(s, depth, unsafe.Pointer(uintptr(p)+field.offset)); err != nil {
return err return err
} }
} else if s.disallowUnknownFields { } else if s.disallowUnknownFields {
return fmt.Errorf("json: unknown field %q", key) return fmt.Errorf("json: unknown field %q", key)
} else { } else {
if err := s.skipValue(); err != nil { if err := s.skipValue(depth); err != nil {
return err return err
} }
} }
@ -547,7 +556,11 @@ func (d *structDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
} }
} }
func (d *structDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) { func (d *structDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
depth++
if depth > maxDecodeNestingDepth {
return 0, errExceededMaxDepth(buf[cursor], cursor)
}
buflen := int64(len(buf)) buflen := int64(len(buf))
cursor = skipWhiteSpace(buf, cursor) cursor = skipWhiteSpace(buf, cursor)
b := (*sliceHeader)(unsafe.Pointer(&buf)).data b := (*sliceHeader)(unsafe.Pointer(&buf)).data
@ -591,13 +604,16 @@ func (d *structDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int6
return 0, errExpected("object value after colon", cursor) return 0, errExpected("object value after colon", cursor)
} }
if field != nil { if field != nil {
c, err := field.dec.decode(buf, cursor, unsafe.Pointer(uintptr(p)+field.offset)) if field.err != nil {
return 0, field.err
}
c, err := field.dec.decode(buf, cursor, depth, unsafe.Pointer(uintptr(p)+field.offset))
if err != nil { if err != nil {
return 0, err return 0, err
} }
cursor = c cursor = c
} else { } else {
c, err := skipValue(buf, cursor) c, err := skipValue(buf, cursor, depth)
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@ -300,7 +300,7 @@ func (u *unmarshalText) UnmarshalText(b []byte) error {
func Test_UnmarshalText(t *testing.T) { func Test_UnmarshalText(t *testing.T) {
t.Run("*struct", func(t *testing.T) { t.Run("*struct", func(t *testing.T) {
var v unmarshalText var v unmarshalText
assertErr(t, json.Unmarshal([]byte(`11`), &v)) assertErr(t, json.Unmarshal([]byte(`"11"`), &v))
assertEq(t, "unmarshal", v.v, 11) assertEq(t, "unmarshal", v.v, 11)
}) })
} }
@ -1063,19 +1063,18 @@ var unmarshalTests = []unmarshalTest{
out: []byteWithPtrMarshalJSON{1, 2, 3}, out: []byteWithPtrMarshalJSON{1, 2, 3},
golden: true, golden: true,
}, },
/* {
{ in: `"AQID"`, // 108
in: `"AQID"`, // 108 ptr: new([]byteWithPtrMarshalText),
ptr: new([]byteWithPtrMarshalText), out: []byteWithPtrMarshalText{1, 2, 3},
out: []byteWithPtrMarshalText{1, 2, 3}, },
}, {
{ in: `["Z01","Z02","Z03"]`, // 109
in: `["Z01","Z02","Z03"]`, // 109 ptr: new([]byteWithPtrMarshalText),
ptr: new([]byteWithPtrMarshalText), out: []byteWithPtrMarshalText{1, 2, 3},
out: []byteWithPtrMarshalText{1, 2, 3}, golden: true,
golden: true, },
},
*/
// ints work with the marshaler but not the base64 []byte case // ints work with the marshaler but not the base64 []byte case
{ {
in: `["Z01","Z02","Z03"]`, // 110 in: `["Z01","Z02","Z03"]`, // 110
@ -2317,7 +2316,6 @@ var decodeTypeErrorTests = []struct {
{new(error), `true`}, {new(error), `true`},
} }
/*
func TestUnmarshalTypeError(t *testing.T) { func TestUnmarshalTypeError(t *testing.T) {
for _, item := range decodeTypeErrorTests { for _, item := range decodeTypeErrorTests {
err := json.Unmarshal([]byte(item.src), item.dest) err := json.Unmarshal([]byte(item.src), item.dest)
@ -2327,7 +2325,6 @@ func TestUnmarshalTypeError(t *testing.T) {
} }
} }
} }
*/
var unmarshalSyntaxTests = []string{ var unmarshalSyntaxTests = []string{
"tru", "tru",
@ -2414,7 +2411,6 @@ func TestSkipArrayObjects(t *testing.T) {
} }
} }
/*
// Test semantics of pre-filled data, such as struct fields, map elements, // Test semantics of pre-filled data, such as struct fields, map elements,
// slices, and arrays. // slices, and arrays.
// Issues 4900 and 8837, among others. // Issues 4900 and 8837, among others.
@ -2468,7 +2464,6 @@ func TestPrefilled(t *testing.T) {
} }
} }
} }
*/
var invalidUnmarshalTests = []struct { var invalidUnmarshalTests = []struct {
v interface{} v interface{}
@ -2479,7 +2474,6 @@ var invalidUnmarshalTests = []struct {
{(*int)(nil), "json: Unmarshal(nil *int)"}, {(*int)(nil), "json: Unmarshal(nil *int)"},
} }
/*
func TestInvalidUnmarshal(t *testing.T) { func TestInvalidUnmarshal(t *testing.T) {
buf := []byte(`{"a":"1"}`) buf := []byte(`{"a":"1"}`)
for _, tt := range invalidUnmarshalTests { for _, tt := range invalidUnmarshalTests {
@ -2493,7 +2487,6 @@ func TestInvalidUnmarshal(t *testing.T) {
} }
} }
} }
*/
var invalidUnmarshalTextTests = []struct { var invalidUnmarshalTextTests = []struct {
v interface{} v interface{}
@ -2505,7 +2498,6 @@ var invalidUnmarshalTextTests = []struct {
{new(net.IP), "json: cannot unmarshal number into Go value of type *net.IP"}, {new(net.IP), "json: cannot unmarshal number into Go value of type *net.IP"},
} }
/*
func TestInvalidUnmarshalText(t *testing.T) { func TestInvalidUnmarshalText(t *testing.T) {
buf := []byte(`123`) buf := []byte(`123`)
for _, tt := range invalidUnmarshalTextTests { for _, tt := range invalidUnmarshalTextTests {
@ -2519,7 +2511,6 @@ func TestInvalidUnmarshalText(t *testing.T) {
} }
} }
} }
*/
/* /*
// Test that string option is ignored for invalid types. // Test that string option is ignored for invalid types.
@ -2547,7 +2538,6 @@ func TestInvalidStringOption(t *testing.T) {
} }
*/ */
/*
// Test unmarshal behavior with regards to embedded unexported structs. // Test unmarshal behavior with regards to embedded unexported structs.
// //
// (Issue 21357) If the embedded struct is a pointer and is unallocated, // (Issue 21357) If the embedded struct is a pointer and is unallocated,
@ -2612,7 +2602,7 @@ func TestUnmarshalEmbeddedUnexported(t *testing.T) {
in: `{"R":2,"Q":1}`, in: `{"R":2,"Q":1}`,
ptr: new(S1), ptr: new(S1),
out: &S1{R: 2}, out: &S1{R: 2},
err: fmt.Errorf("json: cannot set embedded pointer to unexported struct: json.embed1"), err: fmt.Errorf("json: cannot set embedded pointer to unexported struct: json_test.embed1"),
}, { }, {
// The top level Q field takes precedence. // The top level Q field takes precedence.
in: `{"Q":1}`, in: `{"Q":1}`,
@ -2634,7 +2624,7 @@ func TestUnmarshalEmbeddedUnexported(t *testing.T) {
in: `{"R":2,"Q":1}`, in: `{"R":2,"Q":1}`,
ptr: new(S5), ptr: new(S5),
out: &S5{R: 2}, out: &S5{R: 2},
err: fmt.Errorf("json: cannot set embedded pointer to unexported struct: json.embed3"), err: fmt.Errorf("json: cannot set embedded pointer to unexported struct: json_test.embed3"),
}, { }, {
// Issue 24152, ensure decodeState.indirect does not panic. // Issue 24152, ensure decodeState.indirect does not panic.
in: `{"embed1": {"Q": 1}}`, in: `{"embed1": {"Q": 1}}`,
@ -2678,28 +2668,26 @@ func TestUnmarshalEmbeddedUnexported(t *testing.T) {
} }
} }
} }
*/
/*
func TestUnmarshalErrorAfterMultipleJSON(t *testing.T) { func TestUnmarshalErrorAfterMultipleJSON(t *testing.T) {
tests := []struct { tests := []struct {
in string in string
err error err error
}{{ }{{
in: `1 false null :`, in: `1 false null :`,
err: json.NewSyntaxError("invalid character ':' looking for beginning of value", 14), err: json.NewSyntaxError("not at beginning of value", 14),
}, { }, {
in: `1 [] [,]`, in: `1 [] [,]`,
err: json.NewSyntaxError("invalid character ',' looking for beginning of value", 7), err: json.NewSyntaxError("not at beginning of value", 6),
}, { }, {
in: `1 [] [true:]`, in: `1 [] [true:]`,
err: json.NewSyntaxError("invalid character ':' after array element", 11), err: json.NewSyntaxError("json: slice unexpected end of JSON input", 10),
}, { }, {
in: `1 {} {"x"=}`, in: `1 {} {"x"=}`,
err: json.NewSyntaxError("invalid character '=' after object key", 14), err: json.NewSyntaxError("expected colon after object key", 13),
}, { }, {
in: `falsetruenul#`, in: `falsetruenul#`,
err: json.NewSyntaxError("invalid character '#' in literal null (expecting 'l')", 13), err: json.NewSyntaxError("json: invalid character # as null", 12),
}} }}
for i, tt := range tests { for i, tt := range tests {
dec := json.NewDecoder(strings.NewReader(tt.in)) dec := json.NewDecoder(strings.NewReader(tt.in))
@ -2715,7 +2703,6 @@ func TestUnmarshalErrorAfterMultipleJSON(t *testing.T) {
} }
} }
} }
*/
type unmarshalPanic struct{} type unmarshalPanic struct{}
@ -2808,7 +2795,6 @@ func TestUnmarshalRescanLiteralMangledUnquote(t *testing.T) {
} }
} }
/*
func TestUnmarshalMaxDepth(t *testing.T) { func TestUnmarshalMaxDepth(t *testing.T) {
testcases := []struct { testcases := []struct {
name string name string
@ -2888,20 +2874,35 @@ func TestUnmarshalMaxDepth(t *testing.T) {
for _, tc := range testcases { for _, tc := range testcases {
for _, target := range targets { for _, target := range targets {
t.Run(target.name+"-"+tc.name, func(t *testing.T) { t.Run(target.name+"-"+tc.name, func(t *testing.T) {
err := json.Unmarshal([]byte(tc.data), target.newValue()) t.Run("unmarshal", func(t *testing.T) {
if !tc.errMaxDepth { err := json.Unmarshal([]byte(tc.data), target.newValue())
if err != nil { if !tc.errMaxDepth {
t.Errorf("unexpected error: %v", err) if err != nil {
t.Errorf("unexpected error: %v", err)
}
} else {
if err == nil {
t.Errorf("expected error containing 'exceeded max depth', got none")
} else if !strings.Contains(err.Error(), "exceeded max depth") {
t.Errorf("expected error containing 'exceeded max depth', got: %v", err)
}
} }
} else { })
if err == nil { t.Run("stream", func(t *testing.T) {
t.Errorf("expected error containing 'exceeded max depth', got none") err := json.NewDecoder(strings.NewReader(tc.data)).Decode(target.newValue())
} else if !strings.Contains(err.Error(), "exceeded max depth") { if !tc.errMaxDepth {
t.Errorf("expected error containing 'exceeded max depth', got: %v", err) if err != nil {
t.Errorf("unexpected error: %v", err)
}
} else {
if err == nil {
t.Errorf("expected error containing 'exceeded max depth', got none")
} else if !strings.Contains(err.Error(), "exceeded max depth") {
t.Errorf("expected error containing 'exceeded max depth', got: %v", err)
}
} }
} })
}) })
} }
} }
} }
*/

View File

@ -127,7 +127,7 @@ func (d *uintDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error
return nil, 0, errUnexpectedEndOfJSON("number(unsigned integer)", cursor) return nil, 0, errUnexpectedEndOfJSON("number(unsigned integer)", cursor)
} }
func (d *uintDecoder) decodeStream(s *stream, p unsafe.Pointer) error { func (d *uintDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
bytes, err := d.decodeStreamByte(s) bytes, err := d.decodeStreamByte(s)
if err != nil { if err != nil {
return err return err
@ -154,7 +154,7 @@ func (d *uintDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
return nil return nil
} }
func (d *uintDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) { func (d *uintDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
bytes, c, err := d.decodeByte(buf, cursor) bytes, c, err := d.decodeByte(buf, cursor)
if err != nil { if err != nil {
return 0, err return 0, err

View File

@ -28,10 +28,10 @@ func (d *unmarshalJSONDecoder) annotateError(cursor int64, err error) {
} }
} }
func (d *unmarshalJSONDecoder) decodeStream(s *stream, p unsafe.Pointer) error { func (d *unmarshalJSONDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
s.skipWhiteSpace() s.skipWhiteSpace()
start := s.cursor start := s.cursor
if err := s.skipValue(); err != nil { if err := s.skipValue(depth); err != nil {
return err return err
} }
src := s.buf[start:s.cursor] src := s.buf[start:s.cursor]
@ -49,10 +49,10 @@ func (d *unmarshalJSONDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
return nil return nil
} }
func (d *unmarshalJSONDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) { func (d *unmarshalJSONDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
cursor = skipWhiteSpace(buf, cursor) cursor = skipWhiteSpace(buf, cursor)
start := cursor start := cursor
end, err := skipValue(buf, cursor) end, err := skipValue(buf, cursor, depth)
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@ -37,32 +37,38 @@ var (
nullbytes = []byte(`null`) nullbytes = []byte(`null`)
) )
func (d *unmarshalTextDecoder) decodeStream(s *stream, p unsafe.Pointer) error { func (d *unmarshalTextDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
s.skipWhiteSpace() s.skipWhiteSpace()
start := s.cursor start := s.cursor
if err := s.skipValue(); err != nil { if err := s.skipValue(depth); err != nil {
return err return err
} }
src := s.buf[start:s.cursor] src := s.buf[start:s.cursor]
switch src[0] { if len(src) > 0 {
case '[': switch src[0] {
// cannot decode array value by unmarshal text case '[':
return &UnmarshalTypeError{ return &UnmarshalTypeError{
Value: "array", Value: "array",
Type: rtype2type(d.typ), Type: rtype2type(d.typ),
Offset: s.totalOffset(), Offset: s.totalOffset(),
} }
case '{': case '{':
// cannot decode object value by unmarshal text return &UnmarshalTypeError{
return &UnmarshalTypeError{ Value: "object",
Value: "object", Type: rtype2type(d.typ),
Type: rtype2type(d.typ), Offset: s.totalOffset(),
Offset: s.totalOffset(), }
} case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
case 'n': return &UnmarshalTypeError{
if bytes.Equal(src, nullbytes) { Value: "number",
*(*unsafe.Pointer)(p) = nil Type: rtype2type(d.typ),
return nil Offset: s.totalOffset(),
}
case 'n':
if bytes.Equal(src, nullbytes) {
*(*unsafe.Pointer)(p) = nil
return nil
}
} }
} }
dst := make([]byte, len(src)) dst := make([]byte, len(src))
@ -82,17 +88,40 @@ func (d *unmarshalTextDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
return nil return nil
} }
func (d *unmarshalTextDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) { func (d *unmarshalTextDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
cursor = skipWhiteSpace(buf, cursor) cursor = skipWhiteSpace(buf, cursor)
start := cursor start := cursor
end, err := skipValue(buf, cursor) end, err := skipValue(buf, cursor, depth)
if err != nil { if err != nil {
return 0, err return 0, err
} }
src := buf[start:end] src := buf[start:end]
if bytes.Equal(src, nullbytes) { if len(src) > 0 {
*(*unsafe.Pointer)(p) = nil switch src[0] {
return end, nil case '[':
return 0, &UnmarshalTypeError{
Value: "array",
Type: rtype2type(d.typ),
Offset: start,
}
case '{':
return 0, &UnmarshalTypeError{
Value: "object",
Type: rtype2type(d.typ),
Offset: start,
}
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return 0, &UnmarshalTypeError{
Value: "number",
Type: rtype2type(d.typ),
Offset: start,
}
case 'n':
if bytes.Equal(src, nullbytes) {
*(*unsafe.Pointer)(p) = nil
return end, nil
}
}
} }
if s, ok := unquoteBytes(src); ok { if s, ok := unquoteBytes(src); ok {

View File

@ -25,7 +25,7 @@ func newWrappedStringDecoder(typ *rtype, dec decoder, structName, fieldName stri
} }
} }
func (d *wrappedStringDecoder) decodeStream(s *stream, p unsafe.Pointer) error { func (d *wrappedStringDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
bytes, err := d.stringDecoder.decodeStreamByte(s) bytes, err := d.stringDecoder.decodeStreamByte(s)
if err != nil { if err != nil {
return err return err
@ -38,13 +38,13 @@ func (d *wrappedStringDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
} }
b := make([]byte, len(bytes)+1) b := make([]byte, len(bytes)+1)
copy(b, bytes) copy(b, bytes)
if _, err := d.dec.decode(b, 0, p); err != nil { if _, err := d.dec.decode(b, 0, depth, p); err != nil {
return err return err
} }
return nil return nil
} }
func (d *wrappedStringDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) { func (d *wrappedStringDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
bytes, c, err := d.stringDecoder.decodeByte(buf, cursor) bytes, c, err := d.stringDecoder.decodeByte(buf, cursor)
if err != nil { if err != nil {
return 0, err return 0, err
@ -56,7 +56,7 @@ func (d *wrappedStringDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer
return c, nil return c, nil
} }
bytes = append(bytes, nul) bytes = append(bytes, nul)
if _, err := d.dec.decode(bytes, 0, p); err != nil { if _, err := d.dec.decode(bytes, 0, depth, p); err != nil {
return 0, err return 0, err
} }
return c, nil return c, nil

View File

@ -242,7 +242,7 @@ func encodeRunEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, o
ptr := load(ctxptr, code.idx) ptr := load(ctxptr, code.idx)
isPtr := code.typ.Kind() == reflect.Ptr isPtr := code.typ.Kind() == reflect.Ptr
p := ptrToUnsafePtr(ptr) p := ptrToUnsafePtr(ptr)
if p == nil || isPtr && *(*unsafe.Pointer)(p) == nil { if p == nil || isPtr && **(**unsafe.Pointer)(unsafe.Pointer(&p)) == nil {
b = append(b, '"', '"', ',') b = append(b, '"', '"', ',')
} else { } else {
v := *(*interface{})(unsafe.Pointer(&interfaceHeader{ v := *(*interface{})(unsafe.Pointer(&interfaceHeader{

View File

@ -117,6 +117,13 @@ func (e *UnsupportedValueError) Error() string {
return fmt.Sprintf("json: unsupported value: %s", e.Str) return fmt.Sprintf("json: unsupported value: %s", e.Str)
} }
func errExceededMaxDepth(c byte, cursor int64) *SyntaxError {
return &SyntaxError{
msg: fmt.Sprintf(`invalid character "%c" exceeded max depth`, c),
Offset: cursor,
}
}
func errNotAtBeginningOfValue(cursor int64) *SyntaxError { func errNotAtBeginningOfValue(cursor int64) *SyntaxError {
return &SyntaxError{msg: "not at beginning of value", Offset: cursor} return &SyntaxError{msg: "not at beginning of value", Offset: cursor}
} }

View File

@ -376,25 +376,26 @@ var tokenStreamCases = []tokenStreamCase{
map[string]interface{}{"a": float64(1)}, map[string]interface{}{"a": float64(1)},
}}, }},
json.Delim('}')}}, json.Delim('}')}},
{json: ` [{"a": 1} {"a": 2}] `, expTokens: []interface{}{ /*
json.Delim('['), {json: ` [{"a": 1} {"a": 2}] `, expTokens: []interface{}{
decodeThis{map[string]interface{}{"a": float64(1)}}, json.Delim('['),
decodeThis{json.NewSyntaxError("expected comma after array element", 11)}, decodeThis{map[string]interface{}{"a": float64(1)}},
}}, decodeThis{json.NewSyntaxError("expected comma after array element", 11)},
{json: `{ "` + strings.Repeat("a", 513) + `" 1 }`, expTokens: []interface{}{ }},
json.Delim('{'), strings.Repeat("a", 513), {json: `{ "` + strings.Repeat("a", 513) + `" 1 }`, expTokens: []interface{}{
decodeThis{json.NewSyntaxError("expected colon after object key", 518)}, json.Delim('{'), strings.Repeat("a", 513),
}}, decodeThis{json.NewSyntaxError("expected colon after object key", 518)},
{json: `{ "\a" }`, expTokens: []interface{}{ }},
json.Delim('{'), {json: `{ "\a" }`, expTokens: []interface{}{
json.NewSyntaxError("invalid character 'a' in string escape code", 3), json.Delim('{'),
}}, json.NewSyntaxError("invalid character 'a' in string escape code", 3),
{json: ` \a`, expTokens: []interface{}{ }},
json.NewSyntaxError("invalid character '\\\\' looking for beginning of value", 1), {json: ` \a`, expTokens: []interface{}{
}}, json.NewSyntaxError("invalid character '\\\\' looking for beginning of value", 1),
}},
*/
} }
/*
func TestDecodeInStream(t *testing.T) { func TestDecodeInStream(t *testing.T) {
for ci, tcase := range tokenStreamCases { for ci, tcase := range tokenStreamCases {
@ -429,7 +430,6 @@ func TestDecodeInStream(t *testing.T) {
} }
} }
} }
*/
// Test from golang.org/issue/11893 // Test from golang.org/issue/11893
func TestHTTPDecoding(t *testing.T) { func TestHTTPDecoding(t *testing.T) {