Skip to content

Fix NaN handling in getResponseAdForSlot - #1243

Open
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/response-ad-slot-nan-handling
Open

Fix NaN handling in getResponseAdForSlot#1243
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/response-ad-slot-nan-handling

Conversation

@pavankumar-vh

Copy link
Copy Markdown

Overview

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

Bug Description

The function didn't validate that slotIndex is a finite number. If slotIndex was NaN or Infinity, Math.floor(NaN) would return NaN, causing Math.max(0, NaN) to return NaN, and NaN % ads.length to return NaN.

Fix

Added Number.isFinite() check 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 validation

Scope

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

The function didn't validate that slotIndex is a finite number. If slotIndex
was NaN or Infinity, Math.floor(NaN) would return NaN, causing Math.max(0, NaN)
to return NaN, and NaN % ads.length to return NaN.

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

Copy link
Copy Markdown
Contributor

Good catch and a minimally invasive fix. The reasoning is correct: Math.floor(NaN) and Math.floor(Infinity) both propagate through Math.max and % to produce NaN, which would make ads[NaN] return undefined unexpectedly instead of a valid ad. Falling back to 0 for non-finite input is a reasonable, safe default.

A couple of things worth tightening before this lands:

  1. It would help to add a one-line unit test (e.g. in common/src/util/lazy-response-ads.test.ts if one exists, or a new test file) covering NaN, Infinity, and -Infinity inputs. The repo doesn't have existing tests for this function per your own note, but adding one alongside a bug fix like this is the kind of thing a maintainer would expect and it makes the fix easy to verify at a glance.
  2. Minor naming nit: safeSlotIndex is fine, but since you're already calling Math.floor after, you could equally clamp with Number.isFinite(slotIndex) ? Math.floor(slotIndex) : 0 in one line — not required, just a simplification.

Scope is fine, this is a common/ utility and out of the forbidden paths. Small, focused, and worth porting 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