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,13 +31,25 @@ func TestAttribute_EncodeBinary(t *testing.T) {
testserdes.EncodeDecodeBinary(t, attr, new(Attribute))
})
t.Run("NotValidBefore", func(t *testing.T) {
attr := &Attribute{
Type: NotValidBeforeT,
Value: &NotValidBefore{
Height: 123,
},
}
testserdes.EncodeDecodeBinary(t, attr, new(Attribute))
t.Run("positive", func(t *testing.T) {
attr := &Attribute{
Type: NotValidBeforeT,
Value: &NotValidBefore{
Height: 123,
},
}
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) {
getReservedAttribute := func(t AttrType) *Attribute {

View file

@ -2,6 +2,7 @@ package transaction
import (
"encoding/binary"
"fmt"
"github.com/nspcc-dev/neo-go/pkg/io"
)
@ -14,6 +15,13 @@ type NotValidBefore struct {
// DecodeBinary implements io.Serializable interface.
func (n *NotValidBefore) DecodeBinary(br *io.BinReader) {
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)
}