Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:(native) StreamDecoder returns error when run out of buffer while skiping value #267

Merged
merged 3 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions decoder/stream.go
Expand Up @@ -94,11 +94,12 @@ read_more:
}
first = false

if len(buf) > 0 {
l := len(buf)
if l > 0 {
self.Decoder.Reset(string(buf))
err = self.Decoder.Decode(val)
if err != nil {
if ee, ok := err.(SyntaxError); repeat && ok && ee.Code == types.ERR_EOF {
if repeat && repeatable(err) {
goto read_more
}
self.err = err
Expand All @@ -109,7 +110,7 @@ read_more:
self.scanp = 0
}

if len(buf) > p {
if l > p {
// remain undecoded bytes, so copy them into self.buf
self.buf = append(self.buf[:0], buf[p:]...)
} else {
Expand All @@ -124,6 +125,13 @@ read_more:
return err
}

func repeatable(err error) bool {
if ee, ok := err.(SyntaxError); ok && ee.Code == types.ERR_EOF {
return true
}
return false
}

// InputOffset returns the input stream byte offset of the current decoder position.
// The offset gives the location of the end of the most recently returned token and the beginning of the next token.
func (self *StreamDecoder) InputOffset() int64 {
Expand Down
34 changes: 34 additions & 0 deletions decoder/stream_test.go
Expand Up @@ -17,6 +17,7 @@
package decoder

import (
`bytes`
`encoding/json`
`io`
`io/ioutil`
Expand All @@ -35,6 +36,39 @@ var (
strings.Repeat("2",1024)+`"} b {}`
)

func TestStreamError(t *testing.T) {
var qs = []string{
`{`+strings.Repeat(" ", 1024)+`"`,
`{`+strings.Repeat(" ", 1024)+`"}`,
`{`+strings.Repeat(" ", 1024)+`""}`,
`{`+strings.Repeat(" ", 1024)+`"":}`,
`{`+strings.Repeat(" ", 1024)+`"":]`,
`{`+strings.Repeat(" ", 1024)+`"":1x`,
`{`+strings.Repeat(" ", 1024)+`"":1x}`,
`{`+strings.Repeat(" ", 1024)+`"":1x]`,
`{`+strings.Repeat(" ", 1024)+`"":t`,
`{`+strings.Repeat(" ", 1024)+`"":t}`,
`{`+strings.Repeat(" ", 1024)+`"":true]`,
`{`+strings.Repeat(" ", 1024)+`"":f`,
`{`+strings.Repeat(" ", 1024)+`"":f}`,
`{`+strings.Repeat(" ", 1024)+`"":false]`,
`{`+strings.Repeat(" ", 1024)+`"":n`,
`{`+strings.Repeat(" ", 1024)+`"":n}`,
`{`+strings.Repeat(" ", 1024)+`"":null]`,
`{`+strings.Repeat(" ", 1024)+`"":"`,
`{`+strings.Repeat(" ", 1024)+`"":"a`,
`{`+strings.Repeat(" ", 1024)+`"":"a}`,
`{`+strings.Repeat(" ", 1024)+`"":"a"`,
`{`+strings.Repeat(" ", 1024)+`"":"a"]`,
}

for i, q := range qs {
var qq = []byte(q[:1024]+strings.Repeat(" ", i*100)+q[1024:])
var obj interface{}
require.NotNil(t, NewStreamDecoder(bytes.NewReader(qq)).Decode(&obj))
}
}

func TestDecodeEmpty(t *testing.T) {
var str = ``
var r1 = strings.NewReader(str)
Expand Down