1
0
mirror of https://github.com/json-iterator/go.git synced 2025-01-23 18:54:21 +02:00

#153 fix critical bug: infinite loop when write string is invalid utf8

This commit is contained in:
Tao Wen 2017-08-29 23:39:43 +08:00
parent 2dc0031b26
commit f706335302
2 changed files with 16 additions and 3 deletions

View File

@ -286,9 +286,8 @@ func writeStringSlowPathWithHTMLEscaped(stream *Stream, i int, s string, valLen
}
c, size := utf8.DecodeRuneInString(s[i:])
if c == utf8.RuneError && size == 1 {
if start < i {
i++
stream.WriteRaw(s[start:i])
}
start = i
continue
}

View File

@ -5,6 +5,7 @@ import (
"github.com/stretchr/testify/require"
"io"
"testing"
"bytes"
)
func Test_missing_object_end(t *testing.T) {
@ -113,3 +114,16 @@ func Test_chan(t *testing.T) {
should.Nil(err)
should.Equal(``, str)
}
func Test_invalid_number(t *testing.T) {
type Message struct {
Number int `json:"number"`
}
obj := Message{}
decoder := ConfigCompatibleWithStandardLibrary.NewDecoder(bytes.NewBufferString(`{"number":"5"}`))
err := decoder.Decode(&obj)
result, err := ConfigCompatibleWithStandardLibrary.Marshal(err.Error())
should := require.New(t)
should.Nil(err)
should.Contains(string(result), "\xff")
}