From ac41fbec943838d750bc80a0beef508ed4c82134 Mon Sep 17 00:00:00 2001 From: Nao Yonashiro Date: Wed, 25 Aug 2021 07:13:05 +0900 Subject: [PATCH 1/2] fix: fixed buffer length bug on string decoder close #278 --- internal/decoder/string.go | 11 ++++++----- stream_test.go | 14 +++++++++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/internal/decoder/string.go b/internal/decoder/string.go index 50e8835..9e8c737 100644 --- a/internal/decoder/string.go +++ b/internal/decoder/string.go @@ -239,14 +239,15 @@ func stringBytes(s *Stream) ([]byte, error) { fallthrough default: // multi bytes character - r, _ := utf8.DecodeRune(s.buf[cursor:]) - b := []byte(string(r)) + r, size := utf8.DecodeRune(s.buf[cursor:]) if r == utf8.RuneError { - s.buf = append(append(append([]byte{}, s.buf[:cursor]...), b...), s.buf[cursor+1:]...) + s.buf = append(append(append([]byte{}, s.buf[:cursor]...), runeErrBytes...), s.buf[cursor+1:]...) + cursor += runeErrBytesLen + s.length += runeErrBytesLen _, _, p = s.stat() + } else { + cursor += int64(size) } - cursor += int64(len(b)) - s.length += int64(len(b)) continue } cursor++ diff --git a/stream_test.go b/stream_test.go index 054747d..67c91ba 100644 --- a/stream_test.go +++ b/stream_test.go @@ -14,7 +14,6 @@ import ( "net/http" "net/http/httptest" "reflect" - "strings" "testing" @@ -502,3 +501,16 @@ func TestGzipStreaming(t *testing.T) { t.Fatalf("Unexpected error: %v", err) } } + +func TestIssue278(t *testing.T) { + a := `{"嗷嗷":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\u55f7"}` + r := strings.NewReader(a) + var m map[string]string + if err := json.NewDecoder(r).Decode(&m); err != nil { + t.Fatalf("Unexpected error: %v", err) + } + want := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\u55f7" + if got := m["嗷嗷"]; got != want { + t.Errorf("string %q; want = %q", got, want) + } +} From e8637832dd674ed0ab92a36aca6ab1b656c4bd34 Mon Sep 17 00:00:00 2001 From: Nao Yonashiro Date: Wed, 25 Aug 2021 11:25:00 +0900 Subject: [PATCH 2/2] style: gofmt --- internal/decoder/string.go | 2 +- stream_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/decoder/string.go b/internal/decoder/string.go index 5c2389e..65b1004 100644 --- a/internal/decoder/string.go +++ b/internal/decoder/string.go @@ -247,7 +247,7 @@ func stringBytes(s *Stream) ([]byte, error) { } goto ERROR } - r, size := utf8.DecodeRune(s.buf[cursor:]) + 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:]...) cursor += runeErrBytesLen diff --git a/stream_test.go b/stream_test.go index 010b87e..a834e1f 100644 --- a/stream_test.go +++ b/stream_test.go @@ -512,7 +512,7 @@ func TestLongUTF8(t *testing.T) { t.Fatalf("Unexpected error: %v", err) } if got != want { - t.Errorf("string %q; want = %q", got, want) + t.Errorf("string %q; want = %q", got, want) } }