Repository navigation
Conversation
When a huge page number (e.g. PHP_INT_MAX) is requested via ?page=, the
offset calculation `($page - 1) * $perPage` overflows the int type and
becomes a float. Passing that float to `BaseBuilder::limit(?int, ?int)`
raises a TypeError, which `paginate()` reports as a 500 response
("Paginate Error").
Clamp the requested page to the available range, so the offset always
stays within the int type:
$page = max(1, min($page, max(1, $totalPages)));
This also aligns the Query Builder path with the Pager used by the Model
path, which already limits the current page to the page count.
…nate overflow test
This branch has not been deployed
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.
Description
When
?page=contains a very large value (e.g.PHP_INT_MAX), the Query Builder branch ofResponseTrait::paginate()calculates the offset as:That multiplication overflows the
inttype and becomes afloat. BecauseBaseBuilder::limit()is declared aslimit(?int $limit = null, ?int $offset = null), passing the float raises aTypeError, andpaginate()turns it into a500response (RESTful.paginateError).This PR clamps the requested page to the available range before the offset is calculated:
Because
$pagecan no longer exceed$totalPages,$offsetstays bounded by$totaland cannot overflow theinttype. It also makes the Query Builder path consistent with the Model path, where the Pager already limitscurrentPagetopageCount, and with the links, which were already clamped to the available range.meta.pagenow reports the clamped page instead of the out-of-range one.The new regression tests cover both a page overflow with results (page clamped to the last page) and with an empty result set (page clamped to
1, offset0). Without the fix they fail, because the response has nometakey (theTypeErrorwas converted into an error response).No user guide change: the documented behaviour only defines in-range pages, and the clamped page now matches the already-clamped
links.Checklist: