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:
parent
2dc0031b26
commit
f706335302
@ -286,9 +286,8 @@ func writeStringSlowPathWithHTMLEscaped(stream *Stream, i int, s string, valLen
|
|||||||
}
|
}
|
||||||
c, size := utf8.DecodeRuneInString(s[i:])
|
c, size := utf8.DecodeRuneInString(s[i:])
|
||||||
if c == utf8.RuneError && size == 1 {
|
if c == utf8.RuneError && size == 1 {
|
||||||
if start < i {
|
i++
|
||||||
stream.WriteRaw(s[start:i])
|
stream.WriteRaw(s[start:i])
|
||||||
}
|
|
||||||
start = i
|
start = i
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"io"
|
"io"
|
||||||
"testing"
|
"testing"
|
||||||
|
"bytes"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_missing_object_end(t *testing.T) {
|
func Test_missing_object_end(t *testing.T) {
|
||||||
@ -113,3 +114,16 @@ func Test_chan(t *testing.T) {
|
|||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(``, str)
|
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")
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user