From bead25aa6b8b5944be10097f149d761eb41a3724 Mon Sep 17 00:00:00 2001 From: Joakim Olsson Date: Thu, 1 Jan 2026 15:09:00 +0100 Subject: [PATCH] fix: prevent negative slice index when last exceeds items count --- pagination.go | 3 +++ pagination_test.go | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/pagination.go b/pagination.go index 4fc6b0a..ee19c44 100644 --- a/pagination.go +++ b/pagination.go @@ -64,6 +64,9 @@ func GetPage[T any](items []T, first *int, after *string, last *int, before *str } else if last != nil { tmp = *last sIx = len(items) - tmp + if sIx < 0 { + sIx = 0 + } eIx = len(items) } if cursor, err := DecodeCursor(after); err == nil && cursor != "" { diff --git a/pagination_test.go b/pagination_test.go index 0f30590..4c42b81 100644 --- a/pagination_test.go +++ b/pagination_test.go @@ -116,6 +116,17 @@ func TestGetPage(t *testing.T) { EndCursor: ptr("MQ=="), }, }, + { + name: "last exceeds items count", + args: args[string]{items: []string{"1", "2", "3"}, last: ptr(10), max: 10, fn: func(s string) string { return s }}, + wantItems: []string{"1", "2", "3"}, + wantPageInfo: PageInfo{ + StartCursor: ptr("MQ=="), + HasNextPage: false, + HasPreviousPage: false, + EndCursor: ptr("Mw=="), + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { -- 2.52.0