From 44bd60f7e3d54491239e2f7f5c471e3929939cf5 Mon Sep 17 00:00:00 2001 From: Eric Warehime Date: Tue, 4 Mar 2025 12:31:12 -0500 Subject: [PATCH] fix: Fix npe in pagination (#23880) --- CHANGELOG.md | 1 + types/query/collections_pagination.go | 2 +- types/query/collections_pagination_test.go | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 761f7e2015..3a113d22a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i ### Bug Fixes * (x/auth) [#23741](https://github.com/cosmos/cosmos-sdk/pull/23741) Support legacy global AccountNumber. +* (types/query) [#23880](https://github.com/cosmos/cosmos-sdk/pull/23880) Fix NPE in query pagination. ### Removed diff --git a/types/query/collections_pagination.go b/types/query/collections_pagination.go index 902d68b1dd..a9a5b9aa7b 100644 --- a/types/query/collections_pagination.go +++ b/types/query/collections_pagination.go @@ -112,7 +112,7 @@ func CollectionFilteredPaginate[K, V any, C Collection[K, V], T any]( return results, new(PageResponse), nil } // strip the prefix from next key - if len(pageRes.NextKey) != 0 && prefix != nil { + if pageRes != nil && len(pageRes.NextKey) != 0 && prefix != nil { pageRes.NextKey = pageRes.NextKey[len(prefix):] } return results, pageRes, err diff --git a/types/query/collections_pagination_test.go b/types/query/collections_pagination_test.go index 6e4beb8c75..14ff857384 100644 --- a/types/query/collections_pagination_test.go +++ b/types/query/collections_pagination_test.go @@ -2,6 +2,7 @@ package query import ( "context" + "errors" "testing" "github.com/stretchr/testify/require" @@ -15,6 +16,7 @@ func TestCollectionPagination(t *testing.T) { sk, ctx := deps() sb := collections.NewSchemaBuilder(sk) m := collections.NewMap(sb, collections.NewPrefix(0), "_", collections.Uint64Key, collections.Uint64Value) + dummyErr := errors.New("dummy error") for i := uint64(0); i < 300; i++ { require.NoError(t, m.Set(ctx, i, i)) @@ -152,6 +154,18 @@ func TestCollectionPagination(t *testing.T) { {Key: 295, Value: 295}, }, }, + "filtered no key with error": { + req: &PageRequest{ + Limit: 3, + }, + expResp: &PageResponse{ + NextKey: encodeKey(5), + }, + filter: func(key, value uint64) (bool, error) { + return false, dummyErr + }, + wantErr: dummyErr, + }, } for name, tc := range tcs {