فهرست منبع

#178 SkipAndReturnBytes should return copy of memory

Tao Wen 8 سال پیش
والد
کامیت
46b20bbbec
2فایلهای تغییر یافته به همراه29 افزوده شده و 1 حذف شده
  1. 3 1
      feature_iter_skip.go
  2. 26 0
      jsoniter_raw_message_test.go

+ 3 - 1
feature_iter_skip.go

@@ -59,7 +59,9 @@ func (iter *Iterator) stopCapture() []byte {
 	iter.captureStartedAt = -1
 	iter.captured = nil
 	if len(captured) == 0 {
-		return remaining
+		copied := make([]byte, len(remaining))
+		copy(copied, remaining)
+		return copied
 	}
 	captured = append(captured, remaining...)
 	return captured

+ 26 - 0
jsoniter_raw_message_test.go

@@ -3,6 +3,7 @@ package jsoniter
 import (
 	"encoding/json"
 	"github.com/stretchr/testify/require"
+	"strings"
 	"testing"
 )
 
@@ -86,3 +87,28 @@ func Test_marshal_invalid_json_raw_message(t *testing.T) {
 	should.Equal(`{"raw":null}`, string(aout))
 	should.Nil(aouterr)
 }
+
+func Test_raw_message_memory_not_copied_issue(t *testing.T) {
+	jsonStream := `{"name":"xxxxx","bundle_id":"com.zonst.majiang","app_platform":"ios","app_category":"100103", "budget_day":1000,"bidding_min":1,"bidding_max":2,"bidding_type":"CPM", "freq":{"open":true,"type":"day","num":100},"speed":1, "targeting":{"vendor":{"open":true,"list":["zonst"]}, "geo_code":{"open":true,"list":["156110100"]},"app_category":{"open":true,"list":["100101"]}, "day_parting":{"open":true,"list":["100409","100410"]},"device_type":{"open":true,"list":["ipad"]}, "os_version":{"open":true,"list":[10]},"carrier":{"open":true,"list":["mobile"]}, "network":{"open":true,"list":["4G"]}},"url":{"tracking_imp_url":"http://www.baidu.com", "tracking_clk_url":"http://www.baidu.com","jump_url":"http://www.baidu.com","deep_link_url":"http://www.baidu.com"}}`
+	type IteratorObject struct {
+		Name        *string     `json:"name"`
+		BundleId    *string     `json:"bundle_id"`
+		AppCategory *string     `json:"app_category"`
+		AppPlatform *string     `json:"app_platform"`
+		BudgetDay   *float32    `json:"budget_day"`
+		BiddingMax  *float32    `json:"bidding_max"`
+		BiddingMin  *float32    `json:"bidding_min"`
+		BiddingType *string     `json:"bidding_type"`
+		Freq        *RawMessage `json:"freq"`
+		Targeting   *RawMessage `json:"targeting"`
+		Url         *RawMessage `json:"url"`
+		Speed       *int        `json:"speed" db:"speed"`
+	}
+
+	obj := &IteratorObject{}
+	decoder := NewDecoder(strings.NewReader(jsonStream))
+	err := decoder.Decode(obj)
+	should := require.New(t)
+	should.Nil(err)
+	should.Equal(`{"open":true,"type":"day","num":100}`, string(*obj.Freq))
+}