Skip to content

MilestonesPage divides by zero on an unfunded budget and never clamps an over-100% distributed percentage #75

Closed

Description

@chonilius

Overview

src/app/milestones/page.tsx computes each milestone's funding progress with a bare division and no guard against a zero (or exceeded) budget:

{milestones.map((m) => {
  const pct = m.distributed / m.budget;
  return (
    <div key={m.id} ...>
      ...
      <div className="mt-4 h-2 w-full overflow-hidden rounded-full bg-slate-100 dark:bg-slate-800">
        <div
          className="h-full bg-indigo-600"
          style={{ width: `${Math.min(pct * 100, 100)}%` }}
        />
      </div>
      <div className="mt-3 flex items-center justify-between text-sm ...">
        <span>
          {formatCurrency(m.distributed, m.asset)} of{" "}
          {formatCurrency(m.budget, m.asset)}
        </span>
        <span>{formatPercent(pct)}</span>
      </div>
      ...

Two distinct bugs here, both reachable with real backend data given Milestone.budget/distributed are just coerceNonNegative(raw.budget)-derived numbers (see src/lib/adapters.ts:94-106) with no floor above zero and no relationship enforced between the two fields:

  1. Divide-by-zero. A freshly-created milestone with budget: 0 (a legitimate state — a maintainer creates the milestone shell before any sponsor has funded anything, or a milestone's issues were all removed and its budget reset) produces pct = 0 / 0 = NaN in JavaScript. Math.min(NaN * 100, 100) is NaN, so style={{ width: "NaN%" }} is passed to the DOM directly (this line doesn't go through formatPercent) — invalid CSS that browsers silently drop, leaving the progress bar's width at whatever it happened to inherit/default to (typically collapsing to 0 width from the h-2 container with no explicit width, but this is undefined behavior riding on browser CSS-parsing leniency, not a deliberate 0% state). The adjacent text readout, formatPercent(pct), does guard against non-finite values (if (!Number.isFinite(value)) return "0%", see src/lib/utils.ts:40-43), so for budget: 0, distributed: 0 it actually renders "0%" rather than the literal string "NaN%" — which sounds safer but is its own problem, covered next.
  2. The "safe" budget: 0, distributed > 0 case is actually the worse bug. 5 / 0 === Infinity in JavaScript (not NaN — only 0 / 0 is NaN), and Number.isFinite(Infinity) is false, so formatPercent catches this too and also returns "0%". That means a milestone that has actually received real distributed funds against a zero-recorded budget displays "0% — $X of $0 USDC", reading as "nothing has happened yet" when the opposite is true. This is worse than an obviously-broken "NaN%" would be, precisely because it looks plausible instead of visibly wrong.
  3. Unclamped over-100% display. If distributed ever exceeds budget (a real possibility given rounding/timing between multiple sponsors funding concurrently, or a data-entry/backend correction), the progress bar is correctly clamped via Math.min(pct * 100, 100) — but the text readout, formatPercent(pct), has no clamp at all and would show something like "127%" right next to a progress bar that's visually maxed out at 100%, an inconsistent and confusing display of the exact same underlying number.

Requirements

  • Guard the pct calculation against budget === 0 (and, defensively, against budget < 0 if that's ever possible) — render an explicit, honest state (e.g. "Not yet funded" / a 0%-width bar with no percentage text, rather than "NaN%") instead of propagating NaN into a CSS custom property and a text node.
  • Clamp formatPercent(pct)'s displayed value to be consistent with the bar's own Math.min(..., 100) clamp — either clamp inside formatPercent generally (check other call sites first to make sure a >100% clamp is universally correct for all of formatPercent's callers, not just this one) or clamp pct itself before passing it to both the bar width and the text.
  • Consider whether an over-100%-distributed state is itself a signal worth surfacing distinctly (e.g. a small badge/tooltip noting the milestone is over-distributed relative to its budget) rather than merely capping the visual representation — this is a real data-integrity signal for a maintainer/sponsor to know about, not just a rendering nuisance.
  • Apply the same audit to MaintenancePool-related percentage displays if any exist with the same shape (checked: pools.map(...) in the same file does not currently compute a percentage, only raw balance/deposit figures, so no equivalent fix is needed there today — but confirm this holds after any future changes).

Acceptance Criteria

  • A milestone with budget: 0 renders a progress bar and percentage text with no NaN anywhere in the DOM, in a state that's honestly distinguishable from "0% of a real, funded budget."
  • A milestone with distributed > budget renders a percentage text that agrees with the bar's clamped-at-100% visual state (both capped consistently, or both showing the true over-100% figure with a clear "over-distributed" treatment — pick one and apply it to both, don't leave them disagreeing).
  • A normal milestone (0 < distributed <= budget) renders identically to today — no visual regression for the common case.
  • A test covers all three cases: budget: 0, distributed > budget, and the normal case.

Additional Notes

Precise references:

  • src/app/milestones/page.tsx:30 (const pct = m.distributed / m.budget;), :38-43 (bar width, correctly clamped), :44-49 (text readout, formatPercent(pct), not clamped).
  • src/lib/utils.ts:40-43 — formatPercent: if (!Number.isFinite(value)) return "0%"; return \${Math.round(value * 100)}%`;. This is what makes the budget: 0, distributed > 0` case (bug Handle Freighter network-mismatch and wallet-state edge cases before signing #2 above) the one most worth getting right in the fix — it's the case that currently produces a plausible-looking but wrong "0%" rather than an obviously-broken "NaN%".
  • src/lib/adapters.ts:94-106 (adaptMilestone) — confirms budget/distributed are independently coerceNonNegative'd from raw backend strings with no cross-field validation (nothing enforces distributed <= budget or budget > 0 at the adapter layer).

Edge cases: negative budget/distributed are already floored to the adapter's fallback (0) by coerceNonNegative, so true negative values shouldn't reach this component — but re-verify assumptions hold if adaptMilestone is ever changed.

Test/reproduction plan: render the milestones list with three fixture milestones — {budget: 0, distributed: 0}, {budget: 0, distributed: 50}, {budget: 100, distributed: 150} — and assert: no "NaN" substring appears anywhere in the rendered output for any of them; the {budget: 0, distributed: 50} case does not render "0%" (the misleading-Infinity case flagged above); and the {budget: 100, distributed: 150} case's bar width and percentage text agree with each other under whatever clamping policy is chosen.

Activity

  1. added
    bug
    very hard
    Maybe Rewarded
    GrantFox OSS
    Official Campaign | FWC26
    on Aug 16, 2026
  2. added
    Third Campaign
    on Aug 16, 2026
  3. ZuLu0890 commented on Aug 16, 2026

    @ZuLu0890

    I would love to work on this issue

  4. scarface-dev1 commented on Aug 17, 2026

    @scarface-dev1

    please i will love to work on these issue

  5. added
    Stellar Wave
    on Aug 25, 2026
  6. primegangan commented on Aug 25, 2026

    @primegangan

    @primegangan has applied to work on this issue as part of the Stellar Wave Program's 8th wave.

    Hi maintainer, I’m interested in working on this issue. I have experience with similar tasks and can begin immediately.

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @primegangan to this issue.

  7. Muzzy-Cyber commented on Aug 25, 2026

    @Muzzy-Cyber

    @Muzzy-Cyber has applied to work on this issue as part of the Stellar Wave Program's 8th wave.

    Hello, I have carefully gone through this issue and I would definitely like to work on it. Thank you.

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @Muzzy-Cyber to this issue.

  8. Tboy123-emm commented on Aug 25, 2026

    @Tboy123-emm

    @Tboy123-emm has applied to work on this issue as part of the Stellar Wave Program's 8th wave.

    Hello please I'd like to work on this issue

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @Tboy123-emm to this issue.

  9. skaichima commented on Aug 25, 2026

    @skaichima

    @skaichima has applied to work on this issue as part of the Stellar Wave Program's 8th wave.

    Hi! I'd be happy to take this on. I'll guard zero-budget milestones with an explicit state, consistently clamp over-budget progress, and surface over-distribution clearly without affecting normal cases. I'll also verify the maintenance-pool percentage audit and add tests covering zero budget, over-distribution, and normal progress. ETD is in 6 hours tops, please assign

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @skaichima to this issue.

  10. brightcli-stack commented on Aug 25, 2026

    @brightcli-stack
    Contributor

    @brightcli-stack has applied to work on this issue as part of the Stellar Wave Program's 8th wave.

    hello can i work on this issue please

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @brightcli-stack to this issue.

  11. drips-wave commented on Aug 25, 2026

    @drips-wave

    Congratulations, @brightcli-stack! 🎉 Your application was accepted by the repo's maintainers, and the issue is due on August 31, 2026.

    🧑‍💻 @brightcli-stack: Please resolve the issue such that the repo's maintainers have enough time to review your contribution before the due date. You'll earn Points for completing the issue on-time, which will make you eligible for a share of the Stellar Wave Program's reward pool.

    Warning

    When opening a PR, please link it to this issue to ensure it gets tracked accurately. Points are awarded when this issue is marked as completed by the maintainer.

    🤠 Repo maintainers: Please keep an eye on the contributor's progress and review their work before the due date. You can manage this issue, including adjusting its complexity and points, here.

    🌊 Happy Wave 🌊

  12. added a commit that references this issue on Aug 25, 2026
    b6cb677
  13. drips-wave commented on Aug 25, 2026

    @drips-wave

    This issue has been completed by @brightcli-stack as part of the Stellar Wave Program's 8th Wave 🥳

    😎 @brightcli-stack: You earned 200 Points for completing this issue! After the current Wave ends, you'll be eligible for a percentage of the Wave's reward pool based on the percentage of total points you've earned. Learn more here. You can also Leave a review to share your experience working on this issue.

    🧑‍💻 Repo maintainers: How'd the contributor do? Leave a review to share your experience working with them.

  14. added a commit that references this issue on Aug 25, 2026
    572f084
  15. removed
    Stellar Wave
    on Oct 15, 2026
  16. added
    Stellar Wave
    on Oct 15, 2026
  17. removed
    Stellar Wave
    on Oct 15, 2026
  18. added
    Stellar Wave
    on Oct 15, 2026
  19. removed
    Stellar Wave
    on Oct 15, 2026
  20. added
    Stellar Wave
    on Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

GrantFox OSSMaybe RewardedOfficial Campaign | FWC26Stellar WaveThird Campaignbugvery hard

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions