Skip to content

Fix NaN handling in windowFileRead offset and limit - #1242

Open
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/window-file-read-nan-handling
Open

Fix NaN handling in windowFileRead offset and limit#1242
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/window-file-read-nan-handling

Conversation

@pavankumar-vh

Copy link
Copy Markdown

Overview

Fix NaN handling in the windowFileRead function in common/src/util/file-read-limits.ts.

Bug Description

The function didn't validate that offset and limit are finite numbers. If they were NaN or Infinity, Math.floor(NaN ?? 1) would return NaN, causing Math.max(1, NaN) to return NaN.

Fix

Added Number.isFinite() checks to default to safe values for invalid numbers.

Testing

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

Files Changed

  • common/src/util/file-read-limits.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 offset and limit are finite numbers. If they
were NaN or Infinity, Math.floor(NaN ?? 1) would return NaN, causing
Math.max(1, NaN) to return NaN.

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

Copy link
Copy Markdown
Contributor

Good catch. Math.floor(offset ?? 1) only substitutes on null/undefined, so a NaN or Infinity value for offset or limit slips through, and Math.max(1, NaN) returns NaN, silently corrupting the read window. Your Number.isFinite() guard closes that gap correctly, and MAX_READ_FILE_LINES is a sensible fallback for limit.

A few things a maintainer will likely want before landing this:

  • A small test case (e.g. windowFileRead(content, NaN, Infinity)) would be the difference between 'needs-work' and immediate merge — this file evidently has no coverage, and this is exactly the kind of fix that should ship with a regression test to stay fixed.
  • Consider whether callers can actually produce NaN/Infinity here in practice (e.g. from bad JSON/tool-call args) — mentioning that in the PR body strengthens the case for the fix.

Scope and diff size are appropriate — this is a one-file, four-line change to common/, which is fair game per the contributing guide. Substance is right; just needs a test to be fully port-ready.

@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