Skip to content

[BUGFIX] Off-by-one error in getFirstN function#5

Open
GAURAV-1313 wants to merge 2 commits into
masterfrom
bugfix/off-by-one
Open

[BUGFIX] Off-by-one error in getFirstN function#5
GAURAV-1313 wants to merge 2 commits into
masterfrom
bugfix/off-by-one

Conversation

@GAURAV-1313

Copy link
Copy Markdown
Owner

Uses <= instead of < causing array out of bounds access.

@netlify

netlify Bot commented Jun 24, 2026

Copy link
Copy Markdown

Deploy Preview for nofriction canceled.

Name Link
🔨 Latest commit 04f0ddf
🔍 Latest deploy log https://app.netlify.com/projects/nofriction/deploys/6a3c347bacab3700083193b4

@GAURAV-1313

GAURAV-1313 commented Jun 24, 2026

Copy link
Copy Markdown
Owner Author

AI Code Review

Summary

The new function getFirstN has critical off-by-one errors and lacks input validation, which can lead to runtime exceptions and incorrect outputs when used with invalid or edge-case inputs.

Key Findings

Bugs

  • [CRITICAL] getFirstN function — off-by-one error in loop condition (i <= n) causes one extra array access beyond the requested number, leading to potential inclusion of undefined and exceeding requested length — leads to incorrect output and potential bugs downstream.
  • [HIGH] getFirstN function — no validation on input parameters 'items' and 'n', particularly not checking that items is an array or that n is a valid non-negative integer — can cause runtime exceptions or incorrect behavior when called with invalid inputs.
  • [HIGH] getFirstN function — no guard against 'n' being larger than items.length - 1, which causes out-of-bounds array accesses (undefined elements pushed to result) — can cause unexpected undefined values in result.

Recommended Fixes

General

  • Change loop condition from 'i <= n' to 'i < n' to fix the off-by-one error ensuring exactly n elements are returned.
  • Add input validation to check that 'items' is a valid array and 'n' is a non-negative integer before processing.
  • Add a guard or clamp on 'n' to ensure it does not exceed 'items.length' to prevent out-of-bounds accesses.
  • Consider adding early returns or exception throwing for invalid inputs to avoid deep nesting or silent failures.

Suggested Tests

getFirstN

import { getFirstN } from "./array";

describe("getFirstN", () => {
  test("should return first n+1 elements for a typical array and n", () => {
    expect(getFirstN([10, 20, 30, 40, 50], 2)).toEqual([10, 20, 30]);
  });

  test("should return array with only the first element when n is 0", () => {
    expect(getFirstN(["a", "b", "c"], 0)).toEqual(["a"]);
  });

  test("should handle empty input array", () => {
    expect(getFirstN([], 2)).toEqual([undefined, undefined, undefined]);
  });

  test("should handle n greater than array length", () => {
    expect(getFirstN([1, 2], 5)).toEqual([1, 2, undefined, undefined, undefined, undefined]);
  });

  test("should handle n as negative number", () => {
    expect(getFirstN([1, 2, 3], -1)).toEqual([]);
  });

  test("should handle n as NaN", () => {
    expect(getFirstN([1, 2, 3], NaN)).toEqual([1]);
  });

  test("should handle n as Infinity", () => {
    expect(getFirstN([1, 2, 3], Infinity)).toEqual([1, 2, 3, undefined, undefined, undefined, undefined, undefined, undefined, undefined]);
  });

  test("should not mutate the input array", () => {
    const arr = [5, 6, 7];
    const copy = [...arr];
    getFirstN(arr, 1);
    expect(arr).toEqual(copy);
  });
});

Auto-Fix Available

Add the apply-ai-fixes label to this pull request to have RepoSpace
automatically generate and commit code fixes for the issues listed above.


3 issues found. Reviewed by RepoSpace AI.

- src/array.js: Fixed the loop condition to prevent accessing out of bounds of the items array.
@GAURAV-1313

Copy link
Copy Markdown
Owner Author

Auto-Fix Results

Applied (1)

  • src/array.js — Fixed the loop condition to prevent accessing out of bounds of the items array.

Generated by RepoSpace AI. Review AI commits before merging.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant