Skip to content

Commit 486a93a

Browse files
authored
fix: added validation to check if index to fetch element is within limits (#1270)
1 parent 96f1e28 commit 486a93a

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

cmd/propose.go

+6
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ func (*UtilsStruct) Propose(rpcParameters rpc.RPCParameters, config types.Config
113113
return err
114114
}
115115
log.Debug("Propose: Sorted proposed blocks: ", sortedProposedBlocks)
116+
117+
if numOfProposedBlocks <= 0 || len(sortedProposedBlocks) < int(numOfProposedBlocks) {
118+
log.Errorf("Invalid numOfProposedBlocks (%d) or mismatch with sortedProposedBlocks length (%d)", numOfProposedBlocks, len(sortedProposedBlocks))
119+
return errors.New("proposed blocks count mismatch")
120+
}
121+
116122
lastBlockIndex := sortedProposedBlocks[numOfProposedBlocks-1]
117123
log.Debug("Propose: Last block index: ", lastBlockIndex)
118124
lastProposedBlockStruct, err := razorUtils.GetProposedBlock(rpcParameters, epoch, lastBlockIndex)

cmd/propose_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,21 @@ func TestPropose(t *testing.T) {
497497
},
498498
wantErr: errors.New("txnOpts error"),
499499
},
500+
{
501+
name: "Test 20: When there is a mismatch in number of proposed blocks and length of sorted proposed blocks array",
502+
args: args{
503+
state: 2,
504+
staker: bindings.StructsStaker{},
505+
numStakers: 5,
506+
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
507+
biggestStakerId: 2,
508+
salt: saltBytes32,
509+
iteration: 1,
510+
numOfProposedBlocks: 3,
511+
sortedProposedBlockIds: []uint32{2, 1},
512+
},
513+
wantErr: errors.New("proposed blocks count mismatch"),
514+
},
500515
}
501516
for _, tt := range tests {
502517
SetUpMockInterfaces()

0 commit comments

Comments
 (0)