From 23081eadad30d6704f8ff278359501b6dcc4347c Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Fri, 7 May 2021 23:15:16 +0900 Subject: [PATCH 1/3] Fix calculating of length for stream decoder --- decode_map.go | 12 +++--------- decode_stream.go | 9 +++++++++ decode_string.go | 4 +++- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/decode_map.go b/decode_map.go index c09e2a2..a3fa291 100644 --- a/decode_map.go +++ b/decode_map.go @@ -68,10 +68,7 @@ func (d *mapDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) erro return err } s.skipWhiteSpace() - if s.char() == nul { - s.read() - } - if s.char() != ':' { + if !s.equalChar(':') { return errExpected("colon after object key", s.totalOffset()) } s.cursor++ @@ -81,15 +78,12 @@ func (d *mapDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) erro } mapassign(d.mapType, mapValue, k, v) s.skipWhiteSpace() - if s.char() == nul { - s.read() - } - if s.char() == '}' { + if s.equalChar('}') { **(**unsafe.Pointer)(unsafe.Pointer(&p)) = mapValue s.cursor++ return nil } - if s.char() != ',' { + if !s.equalChar(',') { return errExpected("comma after object value", s.totalOffset()) } } diff --git a/decode_stream.go b/decode_stream.go index 0d3e263..ba874e5 100644 --- a/decode_stream.go +++ b/decode_stream.go @@ -49,6 +49,15 @@ func (s *stream) char() byte { return s.buf[s.cursor] } +func (s *stream) equalChar(c byte) bool { + cur := s.buf[s.cursor] + if cur == nul { + s.read() + cur = s.buf[s.cursor] + } + return cur == c +} + func (s *stream) stat() ([]byte, int64, unsafe.Pointer) { return s.buf, s.cursor, (*sliceHeader)(unsafe.Pointer(&s.buf)).data } diff --git a/decode_string.go b/decode_string.go index 6b97c52..fc9aae2 100644 --- a/decode_string.go +++ b/decode_string.go @@ -127,7 +127,8 @@ func decodeUnicode(s *stream) error { unicode := []byte(string(r)) unicodeLen := int64(len(unicode)) s.buf = append(append(s.buf[:s.cursor-1], unicode...), s.buf[s.cursor+offset:]...) - s.length = int64(len(s.buf)) + unicodeOrgLen := offset - 1 + s.length = s.length - (backSlashAndULen + (unicodeOrgLen - unicodeLen)) s.cursor = s.cursor - backSlashAndULen + unicodeLen return nil } @@ -163,6 +164,7 @@ RETRY: return errUnexpectedEndOfJSON("string", s.totalOffset()) } s.buf = append(s.buf[:s.cursor-1], s.buf[s.cursor:]...) + s.length-- s.cursor-- return nil } From ab068b0858a8844a593420fc03f25a3181fe6690 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Fri, 7 May 2021 23:52:24 +0900 Subject: [PATCH 2/3] Remove unnecessary code --- decode_stream.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/decode_stream.go b/decode_stream.go index ba874e5..c5009a3 100644 --- a/decode_stream.go +++ b/decode_stream.go @@ -82,9 +82,6 @@ func (s *stream) readBuf() []byte { copy(s.buf, remainBuf) } remainLen := s.length - s.cursor - if remainLen > 0 { - remainLen-- // last char is nul - } return s.buf[s.cursor+remainLen:] } From f3167760ff0c039fe6536f1b1c5c3c634c634c9d Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Sat, 8 May 2021 01:56:58 +0900 Subject: [PATCH 3/3] Fix stream decoder --- decode_stream.go | 9 ++++++++- decode_string.go | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/decode_stream.go b/decode_stream.go index c5009a3..da75e8f 100644 --- a/decode_stream.go +++ b/decode_stream.go @@ -82,7 +82,14 @@ func (s *stream) readBuf() []byte { copy(s.buf, remainBuf) } remainLen := s.length - s.cursor - return s.buf[s.cursor+remainLen:] + remainNotNulCharNum := int64(0) + for i := int64(0); i < remainLen; i++ { + if s.buf[s.cursor+i] == nul { + break + } + remainNotNulCharNum++ + } + return s.buf[s.cursor+remainNotNulCharNum:] } func (s *stream) read() bool { diff --git a/decode_string.go b/decode_string.go index fc9aae2..8f1b0e5 100644 --- a/decode_string.go +++ b/decode_string.go @@ -213,6 +213,7 @@ func stringBytes(s *stream) ([]byte, error) { s.buf = append(append(append([]byte{}, s.buf[:cursor]...), runeErrBytes...), s.buf[cursor+1:]...) _, _, p = s.stat() cursor += runeErrBytesLen + s.length += runeErrBytesLen continue case nul: s.cursor = cursor @@ -238,6 +239,7 @@ func stringBytes(s *stream) ([]byte, error) { _, _, p = s.stat() } cursor += int64(len(b)) + s.length += int64(len(b)) continue } cursor++