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

Resolve 424: Set (*Stream).bufSize equal to size of (*Stream).buf when it resized #428

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,24 @@ func Test_Decoder_UseNumber(t *testing.T) {
assertEq(t, "json.Number", "json.Number", fmt.Sprintf("%T", v["a"]))
}

func Test_Decoder_UnexpectedEnd(t *testing.T) {
input := []byte("\"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe2\xe200")

dec := json.NewDecoder(bytes.NewReader(input))

for {
_, err := dec.Token()
if err != nil {
var e *json.SyntaxError
if errors.As(err, &e) {
assertEq(t, "error is not expected by test", e.Error(), "json: string unexpected end of JSON input")
break
}
t.Fatal(err)
}
}
}

func Test_Decoder_DisallowUnknownFields(t *testing.T) {
dec := json.NewDecoder(strings.NewReader(`{"x": 1}`))
dec.DisallowUnknownFields()
Expand Down
1 change: 1 addition & 0 deletions internal/decoder/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ func stringBytes(s *Stream) ([]byte, error) {
r, size := utf8.DecodeRune(s.buf[cursor:])
if r == utf8.RuneError {
s.buf = append(append(append([]byte{}, s.buf[:cursor]...), runeErrBytes...), s.buf[cursor+1:]...)
s.bufSize = int64(len(s.buf))
Copy link
Owner

Choose a reason for hiding this comment

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

@vadiminshakov I would like some more explanation as to why the bufSize needs to be changed at this place

Copy link
Author

Choose a reason for hiding this comment

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

We get panic here https://github.com/goccy/go-json/blob/master/internal/decoder/stream.go#L201

Panic occurs because the cursor is pointing out of bounds s.buf.
s.buf slice boundaries are set here: https://github.com/goccy/go-json/blob/master/internal/decoder/stream.go#L195)

But these boundaries do not match where the cursor is pointing. My change makes it so that the cursor does not go beyond the buffer bounds (on s.buf resize we change bufSize accordingly).

cursor += runeErrBytesLen
s.length += runeErrBytesLen
_, _, p = s.stat()
Expand Down