You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2021/01/12 14:05:31 UTC

[dubbo-go-hessian2] branch master updated: Fix #253: Acquire sufficient bytes for string encoding buffers

This is an automated email from the ASF dual-hosted git repository.

alexstocks pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-go-hessian2.git


The following commit(s) were added to refs/heads/master by this push:
     new 0b4c6a8  Fix #253: Acquire sufficient bytes for string encoding buffers
     new c052455  Merge pull request #255 from lujjjh/fix/enc-string-chunk
0b4c6a8 is described below

commit 0b4c6a88a93e706fcc7bf7f6a7de024c5de860a6
Author: lujjjh <lu...@gmail.com>
AuthorDate: Sun Jan 10 00:16:56 2021 +0800

    Fix #253: Acquire sufficient bytes for string encoding buffers
---
 string.go      |  3 ++-
 string_test.go | 10 ++++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/string.go b/string.go
index e01e83e..b8b1f4e 100644
--- a/string.go
+++ b/string.go
@@ -108,7 +108,8 @@ func encString(b []byte, v string) []byte {
 		byteCount int
 	)
 
-	bufp := gxbytes.AcquireBytes(CHUNK_SIZE * 3)
+	// Acquire (CHUNK_SIZE + 1) * 3 bytes since charCount could reach CHUNK_SIZE + 1.
+	bufp := gxbytes.AcquireBytes((CHUNK_SIZE + 1) * 3)
 	defer gxbytes.ReleaseBytes(bufp)
 	buf := *bufp
 
diff --git a/string_test.go b/string_test.go
index 1c2666d..fe034be 100644
--- a/string_test.go
+++ b/string_test.go
@@ -111,6 +111,16 @@ func TestEncRune(t *testing.T) {
 	assertEqual([]byte(res.(string)), []byte(v), t)
 }
 
+func TestEncStringChunk(t *testing.T) {
+	enc := NewEncoder()
+	v := strings.Repeat("我", CHUNK_SIZE-1) + "🤣"
+	assert.Nil(t, enc.Encode(v))
+	dec := NewDecoder(enc.Buffer())
+	s, err := dec.Decode()
+	assert.Nil(t, err)
+	assert.Equal(t, v, s)
+}
+
 func TestString(t *testing.T) {
 	s0 := ""
 	s1 := "0"