From 86411a5ca519fa22d28d7cbfee55f76b94ec7d01 Mon Sep 17 00:00:00 2001 From: Ori Newman Date: Mon, 14 Sep 2020 12:19:46 +0300 Subject: [PATCH] [NOD-1389] Add panic on NumParentBlocks if number of parents is greater than 255 (#928) --- app/appmessage/p2p_blockheader.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/appmessage/p2p_blockheader.go b/app/appmessage/p2p_blockheader.go index 797b3839a..56df2023c 100644 --- a/app/appmessage/p2p_blockheader.go +++ b/app/appmessage/p2p_blockheader.go @@ -6,10 +6,11 @@ package appmessage import ( "fmt" - "io" - "github.com/kaspanet/kaspad/util/daghash" "github.com/kaspanet/kaspad/util/mstime" + "github.com/pkg/errors" + "io" + "math" ) // BaseBlockHeaderPayload is the base number of bytes a block header can be, @@ -60,7 +61,11 @@ type BlockHeader struct { // NumParentBlocks return the number of entries in ParentHashes func (h *BlockHeader) NumParentBlocks() byte { - return byte(len(h.ParentHashes)) + numParents := len(h.ParentHashes) + if numParents > math.MaxUint8 { + panic(errors.Errorf("number of parents is %d, which is more than one byte can fit", numParents)) + } + return byte(numParents) } // BlockHash computes the block identifier hash for the given block header.