From a44cb99df66e8cee17690d77423bfba6610eb59d Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 8 Oct 2020 13:26:18 +0300 Subject: [PATCH] payload: add a check for zero-length address list Which is also present in C# code. Thanks, @AnnaShaleva. --- pkg/network/payload/address.go | 3 +++ pkg/network/payload/address_test.go | 33 +++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/pkg/network/payload/address.go b/pkg/network/payload/address.go index 04e925aed..13e9e9743 100644 --- a/pkg/network/payload/address.go +++ b/pkg/network/payload/address.go @@ -80,6 +80,9 @@ func NewAddressList(n int) *AddressList { // DecodeBinary implements Serializable interface. func (p *AddressList) DecodeBinary(br *io.BinReader) { br.ReadArray(&p.Addrs, MaxAddrsCount) + if len(p.Addrs) == 0 { + br.Err = errors.New("no addresses listed") + } } // EncodeBinary implements Serializable interface. diff --git a/pkg/network/payload/address_test.go b/pkg/network/payload/address_test.go index 6aa29414c..b429a8a24 100644 --- a/pkg/network/payload/address_test.go +++ b/pkg/network/payload/address_test.go @@ -9,6 +9,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/internal/testserdes" "github.com/nspcc-dev/neo-go/pkg/network/capability" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestEncodeDecodeAddress(t *testing.T) { @@ -36,18 +37,38 @@ func TestEncodeDecodeAddress(t *testing.T) { testserdes.EncodeDecodeBinary(t, addr, new(AddressAndTime)) } -func TestEncodeDecodeAddressList(t *testing.T) { - var lenList uint8 = 4 - addrList := NewAddressList(int(lenList)) - for i := 0; i < int(lenList); i++ { - e, _ := net.ResolveTCPAddr("tcp", fmt.Sprintf("127.0.0.1:200%d", i)) - addrList.Addrs[i] = NewAddressAndTime(e, time.Now(), capability.Capabilities{ +func fillAddressList(al *AddressList) { + for i := 0; i < len(al.Addrs); i++ { + e, _ := net.ResolveTCPAddr("tcp", fmt.Sprintf("127.0.0.1:20%d", i)) + al.Addrs[i] = NewAddressAndTime(e, time.Now(), capability.Capabilities{ { Type: capability.TCPServer, Data: &capability.Server{Port: 123}, }, }) } +} +func TestEncodeDecodeAddressList(t *testing.T) { + var lenList uint8 = 4 + addrList := NewAddressList(int(lenList)) + fillAddressList(addrList) testserdes.EncodeDecodeBinary(t, addrList, new(AddressList)) } + +func TestEncodeDecodeBadAddressList(t *testing.T) { + var newAL = new(AddressList) + addrList := NewAddressList(MaxAddrsCount + 1) + fillAddressList(addrList) + + bin, err := testserdes.EncodeBinary(addrList) + require.NoError(t, err) + err = testserdes.DecodeBinary(bin, newAL) + require.Error(t, err) + + addrList = NewAddressList(0) + bin, err = testserdes.EncodeBinary(addrList) + require.NoError(t, err) + err = testserdes.DecodeBinary(bin, newAL) + require.Error(t, err) +}