Skip to content

Commit

Permalink
test: add test for unint64 overflow on limit (cosmos#5713)
Browse files Browse the repository at this point in the history
* test: add test for unint64 overflow.

* add inline comment to mention overflow check.
  • Loading branch information
DimitrisJim authored Jan 24, 2024
1 parent 942efd7 commit bd27f58
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion modules/core/04-channel/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ func (k Keeper) PruneAcknowledgements(ctx sdk.Context, portID, channelID string,
}

start := pruningSequenceStart
end := pruningSequenceStart + limit
end := pruningSequenceStart + limit // note: checked against limit overflowing.
for ; start < end; start++ {
// stop pruning if pruningSequenceStart has reached pruningSequenceEnd, pruningSequenceEnd is
// set to be equal to the _next_ sequence to be sent by the counterparty.
Expand Down
23 changes: 23 additions & 0 deletions modules/core/04-channel/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper_test

import (
"fmt"
"math"
"reflect"
"testing"

Expand Down Expand Up @@ -811,6 +812,28 @@ func (suite *KeeperTestSuite) TestPruneAcknowledgements() {
},
nil,
},
{
"success: limit wraps around due to uint64 overflow",
func() {
// Send 10 packets from B -> A, creating 10 packet receipts and 10 packet acks on A.
suite.sendMockPackets(path, 10, true)
},
func() {
limit = math.MaxUint64
},
func(pruned, left uint64) {
// Nothing should be pruned, by passing in a limit of math.MaxUint64, overflow occurs
// when initializing end to pruningSequenceStart + limit. This results in end always being
// equal to start - 1 and thereby not entering the for loop.
// We expect 10 acks, 10 receipts and pruningSequenceStart == 1 (loop not entered).
postPruneExpState(10, 10, 1)

// We expect 0 to be pruned and 10 left.
suite.Require().Equal(uint64(0), pruned)
suite.Require().Equal(uint64(10), left)
},
nil,
},
{
"failure: packet sequence start not set",
func() {},
Expand Down

0 comments on commit bd27f58

Please # to comment.