From aeb4057ea77faff254b583b90df39a67cc898f26 Mon Sep 17 00:00:00 2001 From: alexstocks <alexstocks@foxmail.com> Date: Sat, 29 Feb 2020 18:56:45 +0800 Subject: [PATCH] Fix: issue 380 --- protocol/dubbo/codec.go | 8 +++++++- protocol/dubbo/codec_test.go | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/protocol/dubbo/codec.go b/protocol/dubbo/codec.go index 6b41d5e7d..1d3cfc2da 100644 --- a/protocol/dubbo/codec.go +++ b/protocol/dubbo/codec.go @@ -75,7 +75,13 @@ func (p *DubboPackage) Marshal() (*bytes.Buffer, error) { } func (p *DubboPackage) Unmarshal(buf *bytes.Buffer, opts ...interface{}) error { - codec := hessian.NewHessianCodec(bufio.NewReaderSize(buf, buf.Len())) + // fix issue https://github.com/apache/dubbo-go/issues/380 + bufLen := buf.Len() + if bufLen < hessian.HEADER_LENGTH { + return perrors.WithStack(hessian.ErrHeaderNotEnough) + } + + codec := hessian.NewHessianCodec(bufio.NewReaderSize(buf, bufLen)) // read header err := codec.ReadHeader(&p.Header) diff --git a/protocol/dubbo/codec_test.go b/protocol/dubbo/codec_test.go index 149644a6e..02ce02d20 100644 --- a/protocol/dubbo/codec_test.go +++ b/protocol/dubbo/codec_test.go @@ -18,6 +18,7 @@ package dubbo import ( + "bytes" "testing" "time" ) @@ -25,6 +26,7 @@ import ( import ( hessian "github.com/apache/dubbo-go-hessian2" "github.com/stretchr/testify/assert" + perrors "github.com/pkg/errors" ) func TestDubboPackage_MarshalAndUnmarshal(t *testing.T) { @@ -72,3 +74,10 @@ func TestDubboPackage_MarshalAndUnmarshal(t *testing.T) { assert.Equal(t, []interface{}{"a"}, pkgres.Body.([]interface{})[5]) assert.Equal(t, map[string]string{"dubbo": "2.0.2", "interface": "Service", "path": "path", "timeout": "1000", "version": "2.6"}, pkgres.Body.([]interface{})[6]) } + +func TestIssue380(t *testing.T) { + pkg := &DubboPackage{} + buf := bytes.NewBuffer([]byte("hello")) + err := pkg.Unmarshal(buf) + assert.True(t, perrors.Cause(err) == hessian.ErrHeaderNotEnough) +} \ No newline at end of file -- GitLab