client: fix Paginate's arguments validation (#6205)

Check both page and defLimit against negative values.

Follow up of #6205

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
This commit is contained in:
Alessio Treglia
2020-05-13 13:14:43 +00:00
committed by GitHub
co-authored by Federico Kunze
parent d9bcca2601
commit 9b6e694a00
2 changed files with 28 additions and 6 deletions
+13 -6
View File
@@ -2,15 +2,22 @@ package client
// Paginate returns the correct starting and ending index for a paginated query,
// given that client provides a desired page and limit of objects and the handler
// provides the total number of objects. If the start page is invalid, non-positive
// values are returned signaling the request is invalid.
//
// NOTE: The start page is assumed to be 1-indexed.
// provides the total number of objects. The start page is assumed to be 1-indexed.
// If the start page is invalid, non-positive values are returned signaling the
// request is invalid; it returns non-positive values if limit is non-positive and
// defLimit is negative.
func Paginate(numObjs, page, limit, defLimit int) (start, end int) {
if page == 0 {
if page <= 0 {
// invalid start page
return -1, -1
} else if limit == 0 {
}
// fallback to default limit if supplied limit is invalid
if limit <= 0 {
if defLimit < 0 {
// invalid default limit
return -1, -1
}
limit = defLimit
}