[BUGFIX] Off-by-one error in getFirstN function#5
Open
GAURAV-1313 wants to merge 2 commits into
Open
Conversation
✅ Deploy Preview for nofriction canceled.
|
Owner
Author
AI Code ReviewSummaryThe 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 FindingsBugs
Recommended FixesGeneral
Suggested Tests
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 AvailableAdd the 3 issues found. Reviewed by RepoSpace AI. |
- src/array.js: Fixed the loop condition to prevent accessing out of bounds of the items array.
Owner
Author
Auto-Fix ResultsApplied (1)
Generated by RepoSpace AI. Review AI commits before merging. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uses <= instead of < causing array out of bounds access.