Skip to content

Fix NaN handling in windowDays calculation - #1253

Open
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/usage-summary-window-days-validation
Open

Fix NaN handling in windowDays calculation#1253
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/usage-summary-window-days-validation

Conversation

@pavankumar-vh

Copy link
Copy Markdown

Overview

Fix NaN handling in windowDays calculation in common/src/util/freebuff-usage-summary.ts.

Bug Description

The function didn't validate that params.windowDays is a finite number. If it was NaN or Infinity, Math.max(1, NaN) would return NaN.

Fix

Added Number.isFinite() check to default to FREEBUFF_USAGE_MAP_DAYS 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-usage-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 params.windowDays is a finite number. If it
was NaN or Infinity, Math.max(1, NaN) would return NaN.

Added Number.isFinite() check to default to FREEBUFF_USAGE_MAP_DAYS for invalid numbers.
@codebuff-team

Copy link
Copy Markdown
Contributor

Good catch and a clean, minimal fix. The original Math.max(1, params.windowDays ?? FREEBUFF_USAGE_MAP_DAYS) indeed produces NaN if a caller passes NaN (or -Infinity), since ?? only falls back on null/undefined. Your Number.isFinite check correctly closes that gap, and common/ is within the accepted contribution scope per the guide.

A few things a maintainer will likely want before porting:

  1. Style nit: the ternary line is quite long and mixes an explicit undefined check with Number.isFinite. Since Number.isFinite(undefined) is false already, you can simplify to Number.isFinite(params.windowDays) ? params.windowDays : FREEBUFF_USAGE_MAP_DAYS (drop the !== undefined check) — Number.isFinite returns false for undefined too, so the extra check is redundant.
  2. No test was added. This is a pure function with a clear input/output contract (buildFreebuffUsageSummary), so a unit test covering NaN, Infinity, and undefined inputs would make this much easier to verify and port safely. The repo likely expects at least one test for a bugfix like this.
  3. Minor naming: safeWindowDays is fine, but consider naming to reflect intent (normalizedWindowDays).

Overall the logic is sound and the change is small and focused — worth a maintainer's time once a test is added.

@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 4, 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