Skip to content

Fix NaN handling in formatPlanUnits - #1241

Open
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/plan-units-nan-handling
Open

Fix NaN handling in formatPlanUnits#1241
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/plan-units-nan-handling

Conversation

@pavankumar-vh

Copy link
Copy Markdown

Overview

Fix NaN handling in the formatPlanUnits function in common/src/util/freebuff-plan-summary.ts.

Bug Description

The function didn't validate that units is a finite number. If units was NaN or Infinity, Math.round(units * 10) / 10 would return NaN, causing the function to return 'NaN' as a string.

Fix

Added Number.isFinite() check to return '0' for invalid numbers.

Testing

No existing tests for this function, but the fix prevents incorrect behavior with invalid inputs.

Files Changed

  • common/src/util/freebuff-plan-summary.ts - Added NaN validation

Scope

This change only touches common/ which is an approved contribution area per the Contributing Guide.

The function didn't validate that units is a finite number. If units was
NaN or Infinity, Math.round(units * 10) / 10 would return NaN, causing
the function to return 'NaN' as a string.

Added Number.isFinite() check to return '0' for invalid numbers.
@codebuff-team

Copy link
Copy Markdown
Contributor

Thanks for the small, focused fix. The change is correct in isolation: Math.round(NaN * 10) / 10 is NaN, and String(NaN) returns 'NaN', which would leak into UI-facing plan summaries. Guarding with Number.isFinite is a reasonable defensive addition, and it's scoped to common/, so it's in bounds for this mirror.

A couple of things a maintainer will likely want before porting:

  1. Where does units actually become NaN/Infinity upstream? If you can point to a real call site (e.g. a division by zero or bad parse somewhere that feeds this function), that strengthens the case a lot - right now the PR is defensive-only, not tied to an observed bug.
  2. Falling back to '0' silently could mask a real upstream data bug rather than surfacing it. Worth a quick gut-check on whether swallowing to '0' is the desired behavior versus, say, throwing or logging in dev.
  3. No test was added. Since this file apparently has no existing tests, adding even one small test case (formatPlanUnits(NaN) === '0') would make this much easier to accept as-is rather than needing follow-up work.

Small, low-risk, and plausible - but light on justification and verification. Worth porting once the above is addressed or at least discussed.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree labels Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants