Skip to content

Fix NaN/Infinity handling in responseAdDisplayCount - #1237

Open
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/response-ad-count-validation
Open

Fix NaN/Infinity handling in responseAdDisplayCount#1237
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/response-ad-count-validation

Conversation

@pavankumar-vh

Copy link
Copy Markdown

Overview

Fix NaN/Infinity handling in the responseAdDisplayCount function in common/src/util/lazy-response-ads.ts.

Bug Description

The function used Math.floor() without checking if the input was a valid number. If params.eligibleCount or params.poolSize was NaN or Infinity, Math.floor() would return NaN, and Math.max(0, NaN) would return NaN, causing incorrect results.

Fix

Added Number.isFinite() checks to default to 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/lazy-response-ads.ts - Added NaN/Infinity validation

Scope

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

The function used Math.floor() without checking if the input was a valid number.
If params.eligibleCount or params.poolSize was NaN or Infinity, Math.floor()
would return NaN, and Math.max(0, NaN) would return NaN, causing incorrect results.

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

Copy link
Copy Markdown
Contributor

Thanks for the contribution. The change itself is small and correct in isolation: guarding against NaN/Infinity before Math.floor is a sound defensive pattern, and the fix doesn't change behavior for valid finite inputs.

A few things that would make this port-ready:

  1. No test coverage. The PR body admits there are no existing tests, but that's an argument for adding one here, not a reason to skip it. A couple of unit tests (responseAdDisplayCount({eligibleCount: NaN, poolSize: 10}), Infinity cases) would make this trivially verifiable and much more likely to be ported as-is.

  2. Root cause unclear. The description doesn't establish where these invalid values would actually originate from - is this a real observed bug (e.g. an upstream division by zero producing NaN) or a purely defensive/theoretical fix? If it's theoretical, it's still reasonable, but worth stating plainly rather than implying it fixes an observed incorrect-behavior case.

  3. Minor: Infinity for poolSize would already be handled correctly by the existing poolSize >= MAX_RESPONSE_AD_POOL_SIZE check before this fix (since Infinity >= MAX_RESPONSE_AD_POOL_SIZE is true), so the practical risk is mostly around NaN. Worth double-checking which case you actually hit.

Add a small test file exercising these edge cases and this becomes an easy merge.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:needs-work Right idea, not mergeable as written 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:needs-work Right idea, not mergeable as written

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants