Skip to content

Fix NaN handling in pct function in db-health-alerts - #1240

Open
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/db-health-alerts-pct-validation
Open

Fix NaN handling in pct function in db-health-alerts#1240
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/db-health-alerts-pct-validation

Conversation

@pavankumar-vh

Copy link
Copy Markdown

Overview

Fix NaN handling in the pct function in common/src/util/db-health-alerts.ts.

Bug Description

The function didn't validate that part and whole are finite numbers. If either was NaN or Infinity, Math.round((part / whole) * 100) would return NaN.

Fix

Added Number.isFinite() checks 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/db-health-alerts.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 part and whole are finite numbers. If either
was NaN or Infinity, Math.round((part / whole) * 100) would return NaN.

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

Copy link
Copy Markdown
Contributor

Good catch. The original whole > 0 ? ... : 0 guard already handled whole being NaN/negative/Infinity, but it didn't protect against part being NaN, which would still propagate through Math.round((part / whole) * 100) and return NaN. Wrapping both operands in Number.isFinite() closes that gap cleanly.

A couple of minor notes for the private port:

  • The whole <= 0 check duplicates part of the old whole > 0 condition; fine as written, but you could simplify to if (!Number.isFinite(part) || !Number.isFinite(whole) || whole <= 0) return 0 (which is what you have) — no change needed, just flagging it's slightly redundant with the finite check for edge cases like whole = -Infinity.
  • Since this is a pure, easily-testable helper, it'd strengthen the PR to add a couple of unit tests (NaN part, NaN whole, whole = 0, whole = Infinity) even though none exist yet for this function — that's usually expected for logic like this and would make the fix self-evidently correct to a reviewer without needing to trace the call sites.

Scope is correct (common/ only), diff is minimal and targeted. Worth porting as-is even without tests, but adding them would make this a stronger PR.

@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