Skip to content

Commit

Permalink
Check for max int bounds when converting from uint
Browse files Browse the repository at this point in the history
  • Loading branch information
algochoi committed Apr 18, 2023
1 parent d68378d commit 430ff14
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions daemon/algod/api/server/v2/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1692,6 +1692,10 @@ func (v2 *Handlers) GetBlockTimeStampOffset(ctx echo.Context) error {
// This is only available in dev mode.
// (POST /v2/devmode/blocks/offset/{offset})
func (v2 *Handlers) SetBlockTimeStampOffset(ctx echo.Context, offset uint64) error {
if offset > math.MaxInt64 {
err := fmt.Errorf("block timestamp offset cannot be larger than max int64 value")
return badRequest(ctx, err, fmt.Sprintf(errFailedSettingTimeStampOffset, err), v2.Log)
}
err := v2.Node.SetBlockTimeStampOffset(int64(offset))
if err != nil {
return badRequest(ctx, err, fmt.Sprintf(errFailedSettingTimeStampOffset, err), v2.Log)
Expand Down
10 changes: 9 additions & 1 deletion daemon/algod/api/server/v2/test/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"errors"
"fmt"
"io"
"math"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -1975,7 +1976,7 @@ func TestTimestampOffsetInDevMode(t *testing.T) {
handler, c, rec, _, _, releasefunc := setupMockNodeForMethodGet(t, cannedStatusReportGolden, true)
defer releasefunc()

// TestSetBlockTimeStampOffset 404
// TestGetBlockTimeStampOffset 404
err := handler.GetBlockTimeStampOffset(c)
require.NoError(t, err)
require.Equal(t, 404, rec.Code)
Expand All @@ -1992,4 +1993,11 @@ func TestTimestampOffsetInDevMode(t *testing.T) {
err = handler.GetBlockTimeStampOffset(c)
require.NoError(t, err)
require.Equal(t, 200, rec.Code)
c, rec = newReq(t)

// TestSetBlockTimeStampOffset 400
err = handler.SetBlockTimeStampOffset(c, math.MaxUint64)
require.NoError(t, err)
require.Equal(t, 400, rec.Code)
require.Equal(t, "{\"message\":\"failed to set timestamp offset on the node: block timestamp offset cannot be larger than max int64 value\"}\n", rec.Body.String())
}

0 comments on commit 430ff14

Please sign in to comment.