From 5466720d757e4643ea11fefe0caada9c57e9e638 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Tue, 31 Oct 2017 15:32:07 -0400 Subject: [PATCH] minor changes from @odeke-em PR #725 --- types/validator_set.go | 5 +++-- types/vote.go | 1 + types/vote_set.go | 4 ++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/types/validator_set.go b/types/validator_set.go index 132957c15..60376a324 100644 --- a/types/validator_set.go +++ b/types/validator_set.go @@ -100,9 +100,10 @@ func (valSet *ValidatorSet) GetByAddress(address []byte) (index int, val *Valida } // GetByIndex returns the validator by index. -// It returns nil values if index >= len(ValidatorSet.Validators) +// It returns nil values if index < 0 or +// index >= len(ValidatorSet.Validators) func (valSet *ValidatorSet) GetByIndex(index int) (address []byte, val *Validator) { - if index >= len(valSet.Validators) { + if index < 0 || index >= len(valSet.Validators) { return nil, nil } val = valSet.Validators[index] diff --git a/types/vote.go b/types/vote.go index 658415682..d5de6348b 100644 --- a/types/vote.go +++ b/types/vote.go @@ -17,6 +17,7 @@ var ( ErrVoteInvalidValidatorAddress = errors.New("Invalid validator address") ErrVoteInvalidSignature = errors.New("Invalid signature") ErrVoteInvalidBlockHash = errors.New("Invalid block hash") + ErrVoteNil = errors.New("Nil vote") ) type ErrVoteConflictingVotes struct { diff --git a/types/vote_set.go b/types/vote_set.go index dcfb0088d..85a839db9 100644 --- a/types/vote_set.go +++ b/types/vote_set.go @@ -123,6 +123,7 @@ func (voteSet *VoteSet) Size() int { // Conflicting votes return added=*, err=ErrVoteConflictingVotes. // NOTE: vote should not be mutated after adding. // NOTE: VoteSet must not be nil +// NOTE: Vote must not be nil func (voteSet *VoteSet) AddVote(vote *Vote) (added bool, err error) { if voteSet == nil { cmn.PanicSanity("AddVote() on nil VoteSet") @@ -135,6 +136,9 @@ func (voteSet *VoteSet) AddVote(vote *Vote) (added bool, err error) { // NOTE: Validates as much as possible before attempting to verify the signature. func (voteSet *VoteSet) addVote(vote *Vote) (added bool, err error) { + if vote == nil { + return false, ErrVoteNil + } valIndex := vote.ValidatorIndex valAddr := vote.ValidatorAddress blockKey := vote.BlockID.Key()