Repository navigation
fix(types): guard nil receiver in ValidatorSet.Size/TotalVotingPower (follow-up to #1331) - #1334
Conversation
…(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]>
📝 WalkthroughWalkthroughThis PR hardens the ChangesValidatorSet nil-receiver safety
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 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 |
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]>
|
Expanded scope: now nil-hardens every receiver-deref method flagged by the scan — 🤖 Co-authored by Claudius the Magnificent AI Agent |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
types/validator_set.go (1)
337-352: 💤 Low valueConsider documenting nil-receiver safety in method doc comments.
Only
Size()received a doc comment update noting its nil-receiver behavior. For consistency and to help callers understand the contract, consider adding similar notes to the other hardened methods:HasProTxHash,GetByProTxHash,GetProTxHashes,GetProTxHashesAsByteArrays,GetPublicKeys,GetProTxHashesOrdered, andIterate.For example:
HasProTxHash: "Returns false if vals is nil."GetByProTxHash: "Returns (-1, nil) if vals is nil."GetProTxHashes,GetProTxHashesAsByteArrays,GetPublicKeys,GetProTxHashesOrdered: "Returns nil if vals is nil."Iterate: "Does nothing if vals is nil."Also applies to: 354-370, 383-393, 395-405, 407-417, 419-430, 495-506
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@types/validator_set.go` around lines 337 - 352, Add nil-receiver safety notes to the doc comments of the other hardened ValidatorSet methods for consistency: update the comment for HasProTxHash to state it "Returns false if vals is nil.", for GetByProTxHash to state it "Returns (-1, nil) if vals is nil.", for GetProTxHashes, GetProTxHashesAsByteArrays, GetPublicKeys, and GetProTxHashesOrdered to state they "Return nil if vals is nil.", and for Iterate to state it "Does nothing if vals is nil."; locate and update the doc comment blocks immediately above the methods HasProTxHash, GetByProTxHash, GetProTxHashes, GetProTxHashesAsByteArrays, GetPublicKeys, GetProTxHashesOrdered, and Iterate so the behavior is clearly documented.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@types/validator_set.go`:
- Around line 337-352: Add nil-receiver safety notes to the doc comments of the
other hardened ValidatorSet methods for consistency: update the comment for
HasProTxHash to state it "Returns false if vals is nil.", for GetByProTxHash to
state it "Returns (-1, nil) if vals is nil.", for GetProTxHashes,
GetProTxHashesAsByteArrays, GetPublicKeys, and GetProTxHashesOrdered to state
they "Return nil if vals is nil.", and for Iterate to state it "Does nothing if
vals is nil."; locate and update the doc comment blocks immediately above the
methods HasProTxHash, GetByProTxHash, GetProTxHashes,
GetProTxHashesAsByteArrays, GetPublicKeys, GetProTxHashesOrdered, and Iterate so
the behavior is clearly documented.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 26ae5a07-0fd7-4302-a8e7-7c29b777d21f
📒 Files selected for processing (2)
types/validator_set.gotypes/validator_set_test.go
Issue being fixed or feature implemented
PR #1331 guarded
GetByIndexagainst a nil*ValidatorSetreceiver.Two sibling methods were missed:
Size()(~line 415):return len(vals.Validators)— bare deref, panics whenvals == nil.TotalVotingPower(): delegates toSize(), inherits the panic.Both are called from consensus hot-paths where a nil validator set is a
plausible (if transient) state.
What was done?
Added a
vals == nilearly-return toSize():TotalVotingPoweris fixed transitively (it delegates toSize). Noother methods were touched — the diff is intentionally minimal.
How Has This Been Tested?
Added
TestValidatorSetNilReceiverSizeAndVotingPowerintypes/validator_set_test.go, matching the pattern ofTestValidatorSetGetByIndexNilReceiverfrom #1331. Both tests userequire.NotPanicsand assert the zero-value return.Full
./types/...suite passes (go test -p 1 ./types/... -tags deadlock).Breaking Changes
None.
Checklist:
🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Tests