From 7421d82c435cbc458f1c619dd7205c905ef99ae7 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Fri, 27 Nov 2020 17:11:53 +0900 Subject: [PATCH] Fix syntax error --- decode_ptr.go | 28 ++++++++++++++++++++++++++++ decode_stream.go | 3 +++ decode_unmarshal_json.go | 20 +++++++++++--------- decode_unmarshal_text.go | 16 +++++++++------- json.go | 2 +- 5 files changed, 52 insertions(+), 17 deletions(-) diff --git a/decode_ptr.go b/decode_ptr.go index bc575f6..c25d893 100644 --- a/decode_ptr.go +++ b/decode_ptr.go @@ -24,6 +24,16 @@ func newPtrDecoder(dec decoder, typ *rtype, structName, fieldName string) *ptrDe func unsafe_New(*rtype) unsafe.Pointer func (d *ptrDecoder) decodeStream(s *stream, p unsafe.Pointer) error { + s.skipWhiteSpace() + if s.char() == nul { + s.read() + } + if s.char() == 'n' { + if err := nullBytes(s); err != nil { + return err + } + return nil + } newptr := unsafe_New(d.typ) *(*unsafe.Pointer)(p) = newptr if err := d.dec.decodeStream(s, newptr); err != nil { @@ -33,6 +43,24 @@ func (d *ptrDecoder) decodeStream(s *stream, p unsafe.Pointer) error { } func (d *ptrDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) { + cursor = skipWhiteSpace(buf, cursor) + if buf[cursor] == 'n' { + buflen := int64(len(buf)) + if cursor+3 >= buflen { + return 0, errUnexpectedEndOfJSON("null", cursor) + } + if buf[cursor+1] != 'u' { + return 0, errInvalidCharacter(buf[cursor+1], "null", cursor) + } + if buf[cursor+2] != 'l' { + return 0, errInvalidCharacter(buf[cursor+2], "null", cursor) + } + if buf[cursor+3] != 'l' { + return 0, errInvalidCharacter(buf[cursor+3], "null", cursor) + } + cursor += 4 + return cursor, nil + } newptr := unsafe_New(d.typ) *(*unsafe.Pointer)(p) = newptr c, err := d.dec.decode(buf, cursor, newptr) diff --git a/decode_stream.go b/decode_stream.go index 962adc0..fdb239e 100644 --- a/decode_stream.go +++ b/decode_stream.go @@ -129,6 +129,9 @@ func (s *stream) skipValue() error { } case ']': bracketCount-- + if braceCount == 0 && bracketCount == 0 { + return nil + } case ',': if bracketCount == 0 && braceCount == 0 { return nil diff --git a/decode_unmarshal_json.go b/decode_unmarshal_json.go index 595b70f..4e7af4c 100644 --- a/decode_unmarshal_json.go +++ b/decode_unmarshal_json.go @@ -19,11 +19,13 @@ func newUnmarshalJSONDecoder(typ *rtype, structName, fieldName string) *unmarsha } } -func (d *unmarshalJSONDecoder) annotateError(err error) { - ut, ok := err.(*UnmarshalTypeError) - if ok { - ut.Struct = d.structName - ut.Field = d.fieldName +func (d *unmarshalJSONDecoder) annotateError(cursor int64, err error) { + switch e := err.(type) { + case *UnmarshalTypeError: + e.Struct = d.structName + e.Field = d.fieldName + case *SyntaxError: + e.Offset = cursor } } @@ -41,7 +43,7 @@ func (d *unmarshalJSONDecoder) decodeStream(s *stream, p unsafe.Pointer) error { ptr: newptr, })) if err := v.(Unmarshaler).UnmarshalJSON(src); err != nil { - d.annotateError(err) + d.annotateError(s.cursor, err) return err } *(*unsafe.Pointer)(p) = newptr @@ -51,7 +53,7 @@ func (d *unmarshalJSONDecoder) decodeStream(s *stream, p unsafe.Pointer) error { ptr: p, })) if err := v.(Unmarshaler).UnmarshalJSON(src); err != nil { - d.annotateError(err) + d.annotateError(s.cursor, err) return err } } @@ -73,7 +75,7 @@ func (d *unmarshalJSONDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer ptr: newptr, })) if err := v.(Unmarshaler).UnmarshalJSON(src); err != nil { - d.annotateError(err) + d.annotateError(cursor, err) return 0, err } *(*unsafe.Pointer)(p) = newptr @@ -83,7 +85,7 @@ func (d *unmarshalJSONDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer ptr: p, })) if err := v.(Unmarshaler).UnmarshalJSON(src); err != nil { - d.annotateError(err) + d.annotateError(cursor, err) return 0, err } } diff --git a/decode_unmarshal_text.go b/decode_unmarshal_text.go index d60b521..3656a8a 100644 --- a/decode_unmarshal_text.go +++ b/decode_unmarshal_text.go @@ -22,11 +22,13 @@ func newUnmarshalTextDecoder(typ *rtype, structName, fieldName string) *unmarsha } } -func (d *unmarshalTextDecoder) annotateError(err error) { - ut, ok := err.(*UnmarshalTypeError) - if ok { - ut.Struct = d.structName - ut.Field = d.fieldName +func (d *unmarshalTextDecoder) annotateError(cursor int64, err error) { + switch e := err.(type) { + case *UnmarshalTypeError: + e.Struct = d.structName + e.Field = d.fieldName + case *SyntaxError: + e.Offset = cursor } } @@ -46,7 +48,7 @@ func (d *unmarshalTextDecoder) decodeStream(s *stream, p unsafe.Pointer) error { ptr: newptr, })) if err := v.(encoding.TextUnmarshaler).UnmarshalText(src); err != nil { - d.annotateError(err) + d.annotateError(s.cursor, err) return err } *(*unsafe.Pointer)(p) = newptr @@ -69,7 +71,7 @@ func (d *unmarshalTextDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer ptr: *(*unsafe.Pointer)(unsafe.Pointer(&p)), })) if err := v.(encoding.TextUnmarshaler).UnmarshalText(src); err != nil { - d.annotateError(err) + d.annotateError(cursor, err) return 0, err } return end, nil diff --git a/json.go b/json.go index 402f07d..9af3529 100644 --- a/json.go +++ b/json.go @@ -329,7 +329,7 @@ func (n Number) MarshalJSON() ([]byte, error) { func (n *Number) UnmarshalJSON(b []byte) error { s := string(b) if _, err := strconv.ParseFloat(s, 64); err != nil { - return err + return &SyntaxError{msg: err.Error()} } *n = Number(s) return nil