core: check the length of NotValidBefore attr while decoding

DecodeBinary throws panic otherwise.
This commit is contained in:
Anna Shaleva 2020-11-18 14:13:09 +03:00
parent 8548786444
commit 31aa66a4a4
2 changed files with 27 additions and 7 deletions

View file

@ -31,6 +31,7 @@ func TestAttribute_EncodeBinary(t *testing.T) {
testserdes.EncodeDecodeBinary(t, attr, new(Attribute)) testserdes.EncodeDecodeBinary(t, attr, new(Attribute))
}) })
t.Run("NotValidBefore", func(t *testing.T) { t.Run("NotValidBefore", func(t *testing.T) {
t.Run("positive", func(t *testing.T) {
attr := &Attribute{ attr := &Attribute{
Type: NotValidBeforeT, Type: NotValidBeforeT,
Value: &NotValidBefore{ Value: &NotValidBefore{
@ -39,6 +40,17 @@ func TestAttribute_EncodeBinary(t *testing.T) {
} }
testserdes.EncodeDecodeBinary(t, attr, new(Attribute)) testserdes.EncodeDecodeBinary(t, attr, new(Attribute))
}) })
t.Run("bad format: too long", func(t *testing.T) {
bw := io.NewBufBinWriter()
bw.WriteVarBytes([]byte{1, 2, 3, 4, 5})
require.Error(t, testserdes.DecodeBinary(bw.Bytes(), new(NotValidBefore)))
})
t.Run("bad format: too short", func(t *testing.T) {
bw := io.NewBufBinWriter()
bw.WriteVarBytes([]byte{1, 2, 3})
require.Error(t, testserdes.DecodeBinary(bw.Bytes(), new(NotValidBefore)))
})
})
t.Run("Reserved", func(t *testing.T) { t.Run("Reserved", func(t *testing.T) {
getReservedAttribute := func(t AttrType) *Attribute { getReservedAttribute := func(t AttrType) *Attribute {
return &Attribute{ return &Attribute{

View file

@ -2,6 +2,7 @@ package transaction
import ( import (
"encoding/binary" "encoding/binary"
"fmt"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
) )
@ -14,6 +15,13 @@ type NotValidBefore struct {
// DecodeBinary implements io.Serializable interface. // DecodeBinary implements io.Serializable interface.
func (n *NotValidBefore) DecodeBinary(br *io.BinReader) { func (n *NotValidBefore) DecodeBinary(br *io.BinReader) {
bytes := br.ReadVarBytes(4) bytes := br.ReadVarBytes(4)
if br.Err != nil {
return
}
if len(bytes) != 4 {
br.Err = fmt.Errorf("expected 4 bytes, got %d", len(bytes))
return
}
n.Height = binary.LittleEndian.Uint32(bytes) n.Height = binary.LittleEndian.Uint32(bytes)
} }