From 2ce8179c8bcf008fab613433ec284517f2fed6a4 Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Tue, 5 Jun 2018 16:20:48 +0400 Subject: [PATCH] validate per_page before page plus an additional check just in case Closes #1688 --- rpc/core/pipe.go | 4 ++++ rpc/core/pipe_test.go | 2 ++ rpc/core/tx.go | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/rpc/core/pipe.go b/rpc/core/pipe.go index e4bb5a294..8330b345d 100644 --- a/rpc/core/pipe.go +++ b/rpc/core/pipe.go @@ -125,6 +125,10 @@ func SetEventBus(b *types.EventBus) { } func validatePage(page, perPage, totalCount int) int { + if perPage < 1 { + return 1 + } + pages := ((totalCount - 1) / perPage) + 1 if page < 1 { page = 1 diff --git a/rpc/core/pipe_test.go b/rpc/core/pipe_test.go index a33e17cf4..225e36492 100644 --- a/rpc/core/pipe_test.go +++ b/rpc/core/pipe_test.go @@ -15,6 +15,8 @@ func TestPaginationPage(t *testing.T) { page int newPage int }{ + {0, 0, 1, 1}, + {0, 10, 0, 1}, {0, 10, 1, 1}, {0, 10, 2, 1}, diff --git a/rpc/core/tx.go b/rpc/core/tx.go index 615136a92..2e27969cc 100644 --- a/rpc/core/tx.go +++ b/rpc/core/tx.go @@ -189,8 +189,8 @@ func TxSearch(query string, prove bool, page, perPage int) (*ctypes.ResultTxSear } totalCount := len(results) - page = validatePage(page, perPage, totalCount) perPage = validatePerPage(perPage) + page = validatePage(page, perPage, totalCount) skipCount := (page - 1) * perPage apiResults := make([]*ctypes.ResultTx, cmn.MinInt(perPage, totalCount-skipCount))