Skip to content

fix(fsm): guard against division by zero in PollsToResults#454

Open
amathxbt wants to merge 1 commit into
canopy-network:mainfrom
amathxbt:fix/gov-poll-results-division-by-zero
Open

fix(fsm): guard against division by zero in PollsToResults#454
amathxbt wants to merge 1 commit into
canopy-network:mainfrom
amathxbt:fix/gov-poll-results-division-by-zero

Conversation

@amathxbt

@amathxbt amathxbt commented Jul 4, 2026

Copy link
Copy Markdown

Bug

In fsm/gov.go, PollsToResults() computes governance poll vote percentages by dividing by TotalTokens without checking whether it is zero:

r.Validators.ApprovePercentage = uint64(float64(r.Validators.ApproveTokens) / float64(r.Validators.TotalTokens) * 100)
...
r.Accounts.ApprovePercentage = uint64(float64(r.Accounts.ApproveTokens) / float64(r.Accounts.TotalTokens) * 100)

Validators.TotalTokens is set from members.TotalPower and Accounts.TotalTokens is set from supply.Total - supply.Staked - dao.Amount. Both can legitimately be 0 (e.g. an empty/zero-power validator committee, or a chain where the entire circulating supply is staked/DAO-held so Accounts.TotalTokens nets to 0).

When TotalTokens == 0, float64(x) / float64(0) produces +Inf (when x > 0) or NaN (when x == 0). Converting either to uint64 is undefined/implementation-defined behavior in Go and produces garbage values (observed as 0 or platform-dependent large numbers), silently corrupting the reported approve/reject/voted percentages returned by this RPC-facing governance function.

Fix

Guard each percentage block with a TotalTokens != 0 check, leaving the percentages at their zero-value default (0) when there is no token base to compute a percentage against. This is a minimal, behavior-preserving change for the non-zero case and eliminates the undefined-behavior float-to-uint64 conversion for the zero case.

Testing

Reviewed the full PollsToResults function end-to-end against the current main branch; the fix only affects the zero-denominator edge case and does not alter the non-zero-denominator control flow or existing percentage math.

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