瀏覽代碼

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

Tao Wen 8 年之前
父節點
當前提交
f706335302
共有 2 個文件被更改,包括 16 次插入3 次删除
  1. 2 3
      feature_stream_string.go
  2. 14 0
      jsoniter_invalid_test.go

+ 2 - 3
feature_stream_string.go

@@ -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 {
-				stream.WriteRaw(s[start:i])
-			}
+			i++
+			stream.WriteRaw(s[start:i])
 			start = i
 			continue
 		}

+ 14 - 0
jsoniter_invalid_test.go

@@ -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")
+}