Repository navigation
fix(consensus): guard nil/out-of-range validator lookup in vote index validation - #1331
Conversation
…idation PR #1329 (commit 8ab6adb) switched addVoteToVoteSetFunc to look up the voter against stateData.Validators (RoundState) instead of stateData.state.Validators. When RoundState.Validators is unset, that turned GetByIndex into a method call on a nil *ValidatorSet, and the len(vals.Validators) bounds check in GetByIndex dereferenced the nil receiver, panicking before the new `if val != nil` guard could help: panic: runtime error: invalid memory address or nil pointer dereference types/validator_set.go:371 internal/consensus/state_add_vote.go:96 internal/consensus/state_add_vote_test.go:162 GetByIndex already documents that it returns nil for an out-of-range index, and its sibling methods (Copy, Equals, IsNilOrEmpty, ...) all guard a nil receiver. This extends GetByIndex to honor the same contract on a nil receiver, returning nil instead of panicking. The nil-check that #1329 added at every call site then handles the result correctly, and all other callers are strictly safer. Adds a regression test asserting GetByIndex never panics on a nil receiver for any index. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthrough
ChangesValidator Set Nil-Receiver Safety
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
This PR fixes a nil-pointer panic in consensus vote processing by making (*types.ValidatorSet).GetByIndex safe to call on a nil receiver, matching the method’s documented “return nil on invalid input” contract, and adds a regression test to ensure it never panics.
Changes:
- Updated
ValidatorSet.GetByIndexto returnnilwhen called on a nil receiver (instead of panicking). - Clarified the method’s doc comment to include the nil-receiver behavior.
- Added a unit test to assert
GetByIndexdoes not panic on a nil receiver across several index values.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
types/validator_set.go |
Adds a nil-receiver guard to GetByIndex and updates its documented contract. |
types/validator_set_test.go |
Adds a regression test ensuring GetByIndex never panics when the receiver is nil. |
| // GetByIndex returns the validator's address and validator itself (copy) by | ||
| // index. | ||
| // It returns nil values if index is less than 0 or greater or equal to | ||
| // len(ValidatorSet.Validators). | ||
| // It returns nil if the receiver is nil, or if index is less than 0 or greater | ||
| // or equal to len(ValidatorSet.Validators). |
…(follow-up to #1331) (#1334) * fix(types): guard nil receiver in ValidatorSet.Size/TotalVotingPower (follow-up to #1331) #1331 hardened GetByIndex but left Size() and TotalVotingPower() with bare nil-pointer dereferences. A nil *ValidatorSet receiver on either method panics at len(vals.Validators). Since TotalVotingPower delegates to Size, a single guard in Size fixes both. Add regression tests matching the GetByIndex nil-receiver test pattern added in #1331. Co-Authored-By: Claude Sonnet 4.6 <[email protected]> * fix(types): nil-harden remaining ValidatorSet receiver methods Guard HasProTxHash, GetByProTxHash, GetProTxHashes, GetProTxHashesAsByteArrays, GetPublicKeys, GetProTxHashesOrdered, and Iterate with a leading `vals == nil` check so callers on a nil set receive the documented zero value instead of a panic. Extends PR #1334 which guarded Size/TotalVotingPower; all previously guarded methods (GetByIndex, Size, Copy, Equals, IsNilOrEmpty, Hash, String variants, MarshalZerologObject, ToProto) are left untouched. Regression tests added in TestValidatorSetNilReceiverHardened (table-driven, one sub-test per newly-hardened method). Co-Authored-By: Claude Sonnet 4.6 <[email protected]> --------- Co-authored-by: Claude Sonnet 4.6 <[email protected]>
Issue being fixed or feature implemented
PR #1329 (merged to
v1.6-devat commit8ab6adbf25294b83d92beae2fbf2572bba918eba) introduced a nil-pointer panic in the vote-add path. CI surfaced it as:Root cause. As part of validating the vote validator index, #1329 switched
addVoteToVoteSetFuncfrom looking the voter up againststateData.state.ValidatorstostateData.Validators(the embeddedRoundStateset). WhenRoundState.Validatorsis unset,vals.GetByIndex(vote.ValidatorIndex)becomes a method call on a nil*ValidatorSet, and thelen(vals.Validators)bounds check insideGetByIndex(types/validator_set.go:371) dereferences the nil receiver. The panic happens insideGetByIndex, before theif val != nilguard #1329 added at the call site can take effect — so that guard never gets a chance to reject the lookup.What was done?
Made
ValidatorSet.GetByIndexhonor its documented contract on a nil receiver. The method already documents that it returns nil for an out-of-range index, and its sibling methods (Copy,Equals,IsNilOrEmpty,VerifyCommit, ...) all guard a nil receiver. This extendsGetByIndexto do the same — returning nil instead of panicking whenvals == nil:The fix is intentionally placed at the exact panic site (
types/validator_set.go:371) rather than at one call site, so it protects every caller. The nil-check that #1329 already added at each call site (state_add_vote.go:96,state_add_vote.go:256,state_apply_commit.go:128) then handles the nil result correctly: an invalid/nil lookup skips the metric / rejects the vote instead of crashing the node.state_add_vote.goitself is left untouched — #1329's guards there are correct onceGetByIndexstops panicking on a nil receiver.A regression test (
TestValidatorSetGetByIndexNilReceiver) assertsGetByIndexnever panics on a nil receiver for negative, zero, in-range, and out-of-range indexes.How Has This Been Tested?
8ab6adbf2):TestAddVote/TestAddVoteToVoteSet/1panicked attypes/validator_set.go:371viastate_add_vote.go:96.internal/consensusAddVote suite (TestAddVote,TestAddVoteValidatorIndexBounds,TestAddVoteVerifyExtensionMwIndexNotInStateValidators) passes with no panic.TestValidatorSetGetByIndexNilReceiverpasses; full./types/package is green.gofmt/lint clean on changed files (types/validator_set.go,types/validator_set_test.go).Commands (CGO + BLS, build tags
tenderdash netgo osusergo):go test ./internal/consensus/ -run 'TestAddVote|TestAddVoteValidatorIndexBounds|TestAddVoteVerifyExtensionMwIndexNotInStateValidators' -vgo test ./types/ -run 'TestValidatorSetBasic|TestValidatorSetGetByIndexNilReceiver' -vgo test ./types/Breaking Changes
None.
GetByIndexalready promised to return nil for invalid input; this only stops it from panicking on a nil receiver. All callers either already nil-check the result or operate on non-nil sets, so behavior is strictly safer.Checklist:
For repository code-owners and collaborators only
🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Tests