Skip to content

fix(core): guard Pagination pageSize against 0/NaN/negative values#3380

Merged
cixzhang merged 1 commit into
facebook:mainfrom
arham766:fix/pagination-pagesize-guard
Jul 2, 2026
Merged

fix(core): guard Pagination pageSize against 0/NaN/negative values#3380
cixzhang merged 1 commit into
facebook:mainfrom
arham766:fix/pagination-pagesize-guard

Conversation

@arham766

@arham766 arham766 commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Implements the fix proposed in #3372. pageSize is typed number, so 0, NaN, and negatives pass the type check, but Math.ceil(totalItems / pageSize) turns them into Infinity/NaN page counts: the dots variant crashes with RangeError: Invalid array length (Array.from({length: Infinity})), compact renders Page 1 of Infinity, and negative values make the component silently render nothing.

  • Pagination.tsx: coerce pageSize to a positive integer at the destructure; non-finite values fall back to the default (10). Same pattern CodeBlock already uses for chunkSize.
  • useTablePagination.tsx: same guard, because the plugin computes totalPages independently and passes it down as an explicit prop, which bypasses Pagination's own coercion.
  • paginateData.ts: same coercion, so a pageSize of 0 no longer slices every page empty underneath a working paginator.
  • Pagination.doc.mjs: one-line note on the coercion (en, zh, dense).

Flooring fractional values also fixes a real mismatch: Array.prototype.slice truncates its arguments, so pageSize={2.5} previously sliced 2 items per page (25 real pages) while the chrome claimed 20, leaving the last items unreachable.

Fixes #3372

Test plan

  • New pageSize guarding block in Pagination.test.tsx: pageSize={0} no longer crashes the dots variant and paginates per item; NaN falls back to the default (Page 1 of 5 for 50 items); -10 clamps to 1; 2.5 floors to 2 (Page 1 of 25).
  • New test in useTablePagination.test.tsx: table plugin with pageSize={0} renders a finite page count (Go to page 5, no Go to page Infinity) and page 1 shows its row instead of an empty slice.
  • npx vitest run packages/core/src/Pagination/Pagination.test.tsx packages/core/src/Table/plugins/pagination/useTablePagination.test.tsx: 90/90 pass.
  • node scripts/check-changesets.mjs passes.

pageSize is typed as number, so 0, NaN, and negatives pass the type check
but Math.ceil(totalItems / pageSize) yields Infinity/NaN page counts, and
Array.from({length: Infinity}) crashes the dots variant. Coerce to a
positive integer at the destructure; the Table pagination plugin gets the
same guard because it computes totalPages independently and passes it down
as an explicit prop, bypassing Pagination's own coercion.

Fixes facebook#3372
@vercel

vercel Bot commented Jul 2, 2026

Copy link
Copy Markdown

@arham766 is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@vercel

vercel Bot commented Jul 2, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
astryx Ready Ready Preview, Comment Jul 2, 2026 6:23am

Request Review

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the report and fix!

@cixzhang cixzhang enabled auto-merge (squash) July 2, 2026 06:21
@cixzhang cixzhang merged commit 3063e75 into facebook:main Jul 2, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Pagination: pageSize={0} crashes the dots variant with RangeError; NaN/negative pageSize renders Infinity/NaN page counts

2 participants