Skip to content

fix: unexpected behavior when buffer ends with backslash #383

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

Merged
merged 7 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3985,3 +3985,23 @@ func TestIssue372(t *testing.T) {
t.Errorf("unexpected result: %v != %v", got, expected)
}
}

type issue384 struct{}

func (t *issue384) UnmarshalJSON(b []byte) error {
return nil
}

func TestIssue384(t *testing.T) {
testcases := []string{
`{"data": "` + strings.Repeat("-", 500) + `\""}`,
`["` + strings.Repeat("-", 508) + `\""]`,
}
for _, tc := range testcases {
dec := json.NewDecoder(strings.NewReader(tc))
var v issue384
if err := dec.Decode(&v); err != nil {
t.Errorf("unexpected error: %v", err)
}
}
}
3 changes: 3 additions & 0 deletions internal/decoder/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ func (s *Stream) skipObject(depth int64) error {
s.cursor = cursor
if s.read() {
_, cursor, p = s.statForRetry()
cursor++
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use s.stat() instead of s.statForRetry() and remove cursor++

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks clearer! I reflected your review in the following commit.

continue
}
return errors.ErrUnexpectedEndOfJSON("string of object", cursor)
Expand Down Expand Up @@ -344,6 +345,7 @@ func (s *Stream) skipArray(depth int64) error {
s.cursor = cursor
if s.read() {
_, cursor, p = s.statForRetry()
Copy link
Contributor

@HurSungYun HurSungYun Jul 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about adding a new parameter rewindCursor in statForRetry? (or make a new function)

func (s *Stream) statForRetry(rewindCursor bool) ([]byte, int64, unsafe.Pointer) {
	if rewindCursor {
		s.cursor-- // for retry ( because caller may progress cursor position in each loop )
	}
	return s.buf, s.cursor, (*sliceHeader)(unsafe.Pointer(&s.buf)).data
}

It may confuse when additional cursor increments happens outside of statForRetry function.

cursor++
continue
}
return errors.ErrUnexpectedEndOfJSON("string of object", cursor)
Expand Down Expand Up @@ -402,6 +404,7 @@ func (s *Stream) skipValue(depth int64) error {
s.cursor = cursor
if s.read() {
_, cursor, p = s.statForRetry()
cursor++
continue
}
return errors.ErrUnexpectedEndOfJSON("value of string", s.totalOffset())
Expand Down