Skip to content

Commit f706335

Browse files
committed
#153 fix critical bug: infinite loop when write string is invalid utf8
1 parent 2dc0031 commit f706335

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

feature_stream_string.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,8 @@ func writeStringSlowPathWithHTMLEscaped(stream *Stream, i int, s string, valLen
286286
}
287287
c, size := utf8.DecodeRuneInString(s[i:])
288288
if c == utf8.RuneError && size == 1 {
289-
if start < i {
290-
stream.WriteRaw(s[start:i])
291-
}
289+
i++
290+
stream.WriteRaw(s[start:i])
292291
start = i
293292
continue
294293
}

jsoniter_invalid_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/stretchr/testify/require"
66
"io"
77
"testing"
8+
"bytes"
89
)
910

1011
func Test_missing_object_end(t *testing.T) {
@@ -113,3 +114,16 @@ func Test_chan(t *testing.T) {
113114
should.Nil(err)
114115
should.Equal(``, str)
115116
}
117+
118+
func Test_invalid_number(t *testing.T) {
119+
type Message struct {
120+
Number int `json:"number"`
121+
}
122+
obj := Message{}
123+
decoder := ConfigCompatibleWithStandardLibrary.NewDecoder(bytes.NewBufferString(`{"number":"5"}`))
124+
err := decoder.Decode(&obj)
125+
result, err := ConfigCompatibleWithStandardLibrary.Marshal(err.Error())
126+
should := require.New(t)
127+
should.Nil(err)
128+
should.Contains(string(result), "\xff")
129+
}

0 commit comments

Comments
 (0)