Skip to content

fix(paginator): leave pagination unchanged when the page size is invalid - #1091

Merged
andrinoff merged 1 commit into
charmbracelet:mainfrom
janearc:fix/paginator-ignore-invalid-page-size
Nov 18, 2026
Merged

andrinoff merged 1 commit into
charmbracelet:mainfrom
janearc:fix/paginator-ignore-invalid-page-size

Conversation

@janearc

@janearc janearc commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Return the existing page count when PerPage is zero or negative, matching the no-op behavior for nonpositive item counts.

Add separate positive, negative, and regression tests to cover valid calculations, preserved state, and the zero-value divide-by-zero panic.

  • [👩🏻‍🎤] I have read CONTRIBUTING.md.
  • [🚳] I have created a discussion that was approved by a maintainer (for new features).

fix(paginator): handle an unset or invalid page size

SetTotalPages divides by PerPage without checking whether it is positive. Calling it with a positive item count on a zero-value paginator, or after New(WithPerPage(0)), therefore panics. A negative page size can also produce an invalid page count.

Return the existing TotalPages without changing the model when PerPage is below one. This follows the method's existing no-op behavior for an item count below one. Positive page sizes retain the current calculation.

The patch includes three separate tests:

  • Positive: valid page sizes calculate full and partial pages correctly.
  • Negative: zero and negative item counts retain their existing no-op behavior and preserve pagination state.
  • Regression: a zero-value model and explicitly configured zero/negative page sizes return the existing count without changing TotalPages, Page or PerPage.

The regression panics for a zero page size on unpatched main; the negative-size case produces a negative page count. Both pass with the fix. The positive and negative controls pass on main and were also shown to fail under their corresponding deliberate breaks.

Open PR #1078 changes the same method for a different input: a valid page size with a shrinking item count. It clamps Page to the new last page after calculating TotalPages, but still divides by PerPage before reaching that clamp. This guard prevents the invalid-page-size failure independently; it does not replace #1078 or fix its out-of-range-current-page case. The two changes are complementary.

The other paginator-related reports found in the issue search concern separate behavior: #810 and #882 address dot-rendering performance; #405, #406 and #503 concern list layout. This patch does not claim to resolve those reports.

The guard was also tested with #1078's clamp and its regression tests in a separate proof checkout; both sets of tests pass together. This submission contains only the invalid-page-size guard, its documentation and its own tests.

Validation on main 1deada954e6e, Go 1.26.10:

  • Full native arm64 race suite passes with -count=4 -cpu=1,4; paginator tests also pass 20 race repetitions.
  • Full amd64 tests and race suite pass under Rosetta.
  • The exact CI-pinned golangci-lint v2.13.1, configured formatting, vet and module tidy checks pass. Module files are unchanged.
  • Linux/Windows 386 (i386), amd64 and arm64, plus Darwin amd64: builds, all-package test compilation, vet and configured lint pass.
  • Aggregate root-module statement coverage increases from 55.9557% (1818/3249) to 55.9865% (1819/3249) with identical atomic coverage settings.

Cross-target binaries were compiled rather than executed. These are local results; remote CI remains to be confirmed.

disclosure: i use ai. i found this bug while working on my own harness with claude. i used claude.. pardon me, i asked claude to root cause, then we worked on the pr you see in front of you, i had codex review it, and kimi said that my skin is looking vibrant today.

Return the existing page count when PerPage is zero or negative,
matching the no-op behavior for nonpositive item counts.

Add separate positive, negative, and regression tests to cover valid
calculations, preserved state, and the zero-value divide-by-zero panic.
@andrinoff
andrinoff merged commit 5629869 into charmbracelet:main Nov 18, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants