From bd27f5806d9b51d1fa460594731941a38dee6fff Mon Sep 17 00:00:00 2001 From: DimitrisJim Date: Wed, 24 Jan 2024 23:31:51 +0200 Subject: [PATCH] test: add test for unint64 overflow on limit (#5713) * test: add test for unint64 overflow. * add inline comment to mention overflow check. --- modules/core/04-channel/keeper/keeper.go | 2 +- modules/core/04-channel/keeper/keeper_test.go | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/modules/core/04-channel/keeper/keeper.go b/modules/core/04-channel/keeper/keeper.go index 67d47cf0685..a65d69266c1 100644 --- a/modules/core/04-channel/keeper/keeper.go +++ b/modules/core/04-channel/keeper/keeper.go @@ -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. diff --git a/modules/core/04-channel/keeper/keeper_test.go b/modules/core/04-channel/keeper/keeper_test.go index 39443cb9f8b..b21b991f864 100644 --- a/modules/core/04-channel/keeper/keeper_test.go +++ b/modules/core/04-channel/keeper/keeper_test.go @@ -2,6 +2,7 @@ package keeper_test import ( "fmt" + "math" "reflect" "testing" @@ -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() {},