From 159d763422673ed71c69ccf2d33140fdb23ee793 Mon Sep 17 00:00:00 2001 From: Sam Kleinman Date: Fri, 14 Jan 2022 09:58:41 -0500 Subject: [PATCH] light: avoid panic for integer underflow (#7589) --- light/proxy/routes.go | 4 +--- light/rpc/client.go | 17 +++++++---------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/light/proxy/routes.go b/light/proxy/routes.go index 4561bf7f9..76cc52f73 100644 --- a/light/proxy/routes.go +++ b/light/proxy/routes.go @@ -16,9 +16,7 @@ type proxyService struct { *lrpc.Client } -func (p proxyService) ABCIQuery(ctx context.Context, path string, data tmbytes.HexBytes, - height int64, prove bool) (*coretypes.ResultABCIQuery, error) { - +func (p proxyService) ABCIQuery(ctx context.Context, path string, data tmbytes.HexBytes, height int64, prove bool) (*coretypes.ResultABCIQuery, error) { return p.ABCIQueryWithOptions(ctx, path, data, rpcclient.ABCIQueryOptions{ Height: height, Prove: prove, diff --git a/light/rpc/client.go b/light/rpc/client.go index 41ed97401..fec6e4723 100644 --- a/light/rpc/client.go +++ b/light/rpc/client.go @@ -565,7 +565,7 @@ func (c *Client) Validators( } skipCount := validateSkipCount(page, perPage) - v := l.ValidatorSet.Validators[skipCount : skipCount+tmmath.MinInt(perPage, totalCount-skipCount)] + v := l.ValidatorSet.Validators[skipCount : skipCount+tmmath.MinInt(int(perPage), totalCount-skipCount)] return &coretypes.ResultValidators{ BlockHeight: l.Height, @@ -672,16 +672,13 @@ const ( maxPerPage = 100 ) -func validatePage(pagePtr *int, perPage, totalCount int) (int, error) { - if perPage < 1 { - panic(fmt.Errorf("%w (%d)", coretypes.ErrZeroOrNegativePerPage, perPage)) - } +func validatePage(pagePtr *int, perPage uint, totalCount int) (int, error) { if pagePtr == nil { // no page parameter return 1, nil } - pages := ((totalCount - 1) / perPage) + 1 + pages := ((totalCount - 1) / int(perPage)) + 1 if pages == 0 { pages = 1 // one page (even if it's empty) } @@ -693,7 +690,7 @@ func validatePage(pagePtr *int, perPage, totalCount int) (int, error) { return page, nil } -func validatePerPage(perPagePtr *int) int { +func validatePerPage(perPagePtr *int) uint { if perPagePtr == nil { // no per_page parameter return defaultPerPage } @@ -704,11 +701,11 @@ func validatePerPage(perPagePtr *int) int { } else if perPage > maxPerPage { return maxPerPage } - return perPage + return uint(perPage) } -func validateSkipCount(page, perPage int) int { - skipCount := (page - 1) * perPage +func validateSkipCount(page int, perPage uint) int { + skipCount := (page - 1) * int(perPage) if skipCount < 0 { return 0 }