Skip to content

range() throws RangeError when called with single argument because stop check uses strict equality against null #23197

Open

Description

@prudhvirajkonda8-bot

Description

In app/src/lib/range.ts:

export function range(
  start: number,
  stop: number,
  step?: number
): ReadonlyArray<number> {
  if (stop === null) {
    stop = start || 0
    start = 0
  }

The comment notes that the function is extracted from underscore.js (https://github.com/jashkenas/underscore/blob/fc039f6a94fcf388d2b61ced4c02cd1ba116ecfd/underscore.js#L693-L710).

In underscore.js, range supports single-argument invocation (range(stop), such as range(4) yielding [0, 1, 2, 3]), checking if (stop == null).

In app/src/lib/range.ts:

  1. stop is not marked optional in the signature (stop: number instead of stop?: number | null).
  2. The condition if (stop === null) uses strict equality against null. When invoked with a single argument, stop is undefined, so stop === null evaluates to false.
  3. Consequently, Math.ceil((stop - start) / step) computes Math.ceil((undefined - start) / 1) which is NaN.
  4. Calling new Array<number>(NaN) throws RangeError: Array length must be a positive integer of safe magnitude.

Steps to reproduce

import { range } from './lib/range'

range(4) // Throws RangeError: Array length must be a positive integer of safe magnitude

Proposed fix

  1. Update stop parameter type to stop?: number | null.
  2. Change the check to if (stop == null).
  3. Add unit tests in app/test/unit/range-test.ts covering single argument, start/stop, positive/negative steps, and empty ranges.

Activity

  1. prudhvirajkonda8-bot commented on Nov 17, 2026

    @prudhvirajkonda8-bot
    Author

    A fix with comprehensive unit tests is implemented and pushed to branch:
    https://github.com/prudhvirajkonda8-bot/desktop/tree/fix/range-single-argument

    Changes:

    • Update parameter type to stop?: number | null
    • Change if (stop === null) to if (stop == null) so omitting stop correctly sets stop = start || 0 and start = 0
    • Add unit test suite in app/test/unit/range-test.ts verifying range(stop), range(start, stop), stepping, and null handling.
  2. added
    bug
    priority-3
    on Nov 17, 2026
  3. desktop-triage commented on Nov 17, 2026

    @desktop-triage

    Triage summary: No duplicates found in a search of desktop/desktop for similar range()/RangeError reports. This is a confirmed bug, requesting bug — HIGH confidence — and priority-3 — MEDIUM confidence, since the current codebase has no call site that invokes range() with a single argument, limiting real-world impact today.

    Initial hypothesis

    At commit 01cde4fd4d7bb07e642bcbf330e3f1bf3180ef38, range() checks if (stop === null) to detect the single-argument call form, but when range() is called with one argument stop is undefined, not null, so the branch is never entered; stop remains undefined, Math.ceil((stop - start) / step) evaluates to NaN, and new Array<number>(NaN) throws RangeError. This matches underscore.js's original stop == null (loose equality, matching both null and undefined), which the ported version narrowed to ===, silently breaking the single-argument form while keeping the two/three-argument forms working. A search across app/src shows every current call site (app/src/ui/lib/list/list.tsx, app/src/ui/lib/list/section-list.tsx) passes both start and stop, so this is a correctness/API-contract defect in the utility rather than a currently user-visible crash; it would surface the moment any caller (new or future) uses the single-argument form. The reporter's proposed fix (stop?: number | null plus stop == null) directly addresses the root cause.

    Generated by Issue Triage (skills-driven) for #23197 · auto · 20.2 AIC · ⊞ 4.7K · ◷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpriority-3

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions