Skip to content

fix: use dynamic array size for IRV tie handling#251

Open
ArshLabs wants to merge 1 commit into
AOSSIE-Org:mainfrom
ArshLabs:fix/irv-tie-array-size
Open

fix: use dynamic array size for IRV tie handling#251
ArshLabs wants to merge 1 commit into
AOSSIE-Org:mainfrom
ArshLabs:fix/irv-tie-array-size

Conversation

@ArshLabs

@ArshLabs ArshLabs commented Mar 13, 2026

Copy link
Copy Markdown

Problem

In IRVResult.sol, the winners array on the tie path is allocated with a hardcoded size of 2:

winners = new uint256[](2);

If 3 or more candidates are tied during an IRV elimination round, only the first 2 make it into the result array. The rest are silently dropped, which gives wrong election outcomes.

Fix

Changed the allocation to use count, which is already computed in the same scope as the number of tied candidates:

winners = new uint256[](count);

No logic changes, just the array size is corrected.

Found this while writing edge case tests for 3-way ties.

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced accuracy of voting calculations for tied results. Previously, when multiple candidates received the same highest vote count, the system would return results in a fixed-size array regardless of the actual number of ties. It now correctly allocates appropriate space for all tied winners, ensuring accurate and precise election outcome reporting.

IRVResult.sol allocates the winners array with a hardcoded size of 2
on the tie path. If 3 or more candidates are tied during IRV
elimination, only 2 get returned and the rest are silently dropped.

This gives wrong election results for any 3+ way tie.

Changed new uint256[](2) to new uint256[](count) where count is the
number of tied candidates already computed in the same scope.
@coderabbitai

coderabbitai Bot commented Mar 13, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 13afea3c-50c7-4d28-897e-132de70fdccc

📥 Commits

Reviewing files that changed from the base of the PR and between e4df55f and 7885bfa.

📒 Files selected for processing (1)
  • blockchain/contracts/resultCalculators/IRVResult.sol

📝 Walkthrough

Walkthrough

This change modifies the tie-handling logic in an IRV (Instant Runoff Voting) result calculator smart contract. The winners array allocation during tie scenarios now uses the actual count of tied candidates instead of a fixed size, ensuring the returned array accurately reflects all tied winners.

Changes

Cohort / File(s) Summary
Tie-Handling Array Allocation
blockchain/contracts/resultCalculators/IRVResult.sol
Modified winners array allocation to use dynamic size based on actual tied candidate count instead of fixed size of 2.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A tie that tangled in the vote,
Now counted with a broader note,
Each winner gets their rightful place,
No more are two the default space! 🥕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: use dynamic array size for IRV tie handling' accurately describes the main change: replacing a hardcoded array size of 2 with a dynamic size based on the actual count of tied candidates.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 Coding Plan
  • Generate coding plan for human review comments

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.

Tip

You can customize the high-level summary generated by CodeRabbit.

Configure the reviews.high_level_summary_instructions setting to provide custom instructions for generating the high-level summary.

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