From 526bddfa92f2a4a91bee1a6cf7bc174989f39c8a Mon Sep 17 00:00:00 2001 From: DimitrisJim Date: Wed, 24 Jan 2024 16:54:30 +0200 Subject: [PATCH 1/2] test: add test for unint64 overflow. --- modules/core/04-channel/keeper/keeper_test.go | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/modules/core/04-channel/keeper/keeper_test.go b/modules/core/04-channel/keeper/keeper_test.go index 3674ae2c457..c402a6eab0b 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() {}, From c8bfd571eb9f17384e3b169f953c42b289bc56cf Mon Sep 17 00:00:00 2001 From: DimitrisJim Date: Wed, 24 Jan 2024 18:30:25 +0200 Subject: [PATCH 2/2] add inline comment to mention overflow check. --- modules/core/04-channel/keeper/keeper.go | 2 +- 1 file changed, 1 insertion(+), 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.