Merge pull request #279 from orisano/fix-utf-8-handling

fix: fixed invalid utf8 on stream decoder
This commit is contained in:
Masaaki Goshima 2021-08-25 11:19:49 +09:00 committed by GitHub
commit 8ebef3b42d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -239,6 +239,14 @@ func stringBytes(s *Stream) ([]byte, error) {
fallthrough fallthrough
default: default:
// multi bytes character // multi bytes character
if !utf8.FullRune(s.buf[cursor : len(s.buf)-1]) {
s.cursor = cursor
if s.read() {
_, cursor, p = s.stat()
continue
}
goto ERROR
}
r, _ := utf8.DecodeRune(s.buf[cursor:]) r, _ := utf8.DecodeRune(s.buf[cursor:])
b := []byte(string(r)) b := []byte(string(r))
if r == utf8.RuneError { if r == utf8.RuneError {

View File

@ -14,7 +14,7 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"reflect" "reflect"
"strconv"
"strings" "strings"
"testing" "testing"
@ -502,3 +502,16 @@ func TestGzipStreaming(t *testing.T) {
t.Fatalf("Unexpected error: %v", err) t.Fatalf("Unexpected error: %v", err)
} }
} }
func TestLongUTF8(t *testing.T) {
want := strings.Repeat("あ", 342)
r := strings.NewReader(strconv.Quote(want))
var got string
if err := json.NewDecoder(r).Decode(&got); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if got != want {
t.Errorf("string %q; want = %q", got, want)
}
}