Skip to content

fix(types): guard nil receiver in ValidatorSet.Size/TotalVotingPower (follow-up to #1331) - #1334

Merged
lklimek merged 3 commits into
v1.6-devfrom
fix/validator-set-nil-receiver-hardening
Jun 12, 2026
Merged

lklimek merged 3 commits into
v1.6-devfrom
fix/validator-set-nil-receiver-hardening

Conversation

@lklimek

@lklimek lklimek commented Jun 12, 2026 •

Copy link
Copy Markdown
Collaborator

Issue being fixed or feature implemented

PR #1331 guarded GetByIndex against a nil *ValidatorSet receiver.
Two sibling methods were missed:

  • Size() (~line 415): return len(vals.Validators) — bare deref, panics when vals == nil.
  • TotalVotingPower(): delegates to Size(), 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 == nil early-return to Size():

func (vals *ValidatorSet) Size() int {
    if vals == nil {
        return 0
    }
    return len(vals.Validators)
}

TotalVotingPower is fixed transitively (it delegates to Size). No
other methods were touched — the diff is intentionally minimal.

How Has This Been Tested?

Added TestValidatorSetNilReceiverSizeAndVotingPower in
types/validator_set_test.go, matching the pattern of
TestValidatorSetGetByIndexNilReceiver from #1331. Both tests use
require.NotPanics and assert the zero-value return.

Full ./types/... suite passes (go test -p 1 ./types/... -tags deadlock).

Breaking Changes

None.

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have made corresponding changes to the documentation

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced stability of validator set operations with defensive checks to prevent potential crashes during edge cases.
  • Tests

    • Expanded test coverage for validator set edge cases to ensure operational robustness.

…(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]>
@lklimek
lklimek requested a review from QuantumExplorer as a code owner June 12, 2026 10:49
@coderabbitai

coderabbitai Bot commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR hardens the ValidatorSet type by adding nil-receiver defensive checks to eight methods and provides corresponding test coverage. When called on a nil receiver, methods now return safe empty results (false, nil, zero) instead of panicking from nil dereference.

Changes

ValidatorSet nil-receiver safety

Layer / File(s) Summary
Nil-receiver defensive checks in ValidatorSet methods
types/validator_set.go
Methods HasProTxHash, GetByProTxHash, GetProTxHashes, GetProTxHashesAsByteArrays, GetPublicKeys, GetProTxHashesOrdered, Size, and Iterate now check for nil receivers and return safe defaults: false, -1/nil, nil slices, zero, or become a no-op.
Test coverage for nil-receiver behavior
types/validator_set_test.go
Two new tests verify nil-receiver method calls return zero values without panicking: one test for Size() and TotalVotingPower(), and one table-driven test asserting multiple hardened methods behave safely on nil receivers.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

Suggested reviewers

  • QuantumExplorer

Poem

🐰 Nil receivers once caused panics most dire,
But now ValidatorSet methods won't expire!
Safe defaults returned, no dereference spree,
Tests confirm all is well and panic-free! 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title specifically identifies the main change (nil receiver guard in ValidatorSet.Size/TotalVotingPower) and references follow-up context (#1331), matching the primary objective of the PR.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/validator-set-nil-receiver-hardening

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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]>
@lklimek

lklimek commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator Author

Expanded scope: now nil-hardens every receiver-deref method flagged by the scan — HasProTxHash(→false), GetByProTxHash(→-1,nil), GetProTxHashes/GetProTxHashesAsByteArrays/GetProTxHashesOrdered/GetPublicKeys(→nil), Iterate(→no-op) — plus the original Size()/TotalVotingPower(). Added a 7-case nil-receiver table test; ./types/... green.

🤖 Co-authored by Claudius the Magnificent AI Agent

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
types/validator_set.go (1)

337-352: 💤 Low value

Consider 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, and Iterate.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 2504c1f and 9d980ac.

📒 Files selected for processing (2)
  • types/validator_set.go
  • types/validator_set_test.go

@lklimek
lklimek merged commit b0672fc into v1.6-dev Jun 12, 2026
15 checks passed
@lklimek
lklimek deleted the fix/validator-set-nil-receiver-hardening branch June 12, 2026 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant