From 3294103e06e95fb72f0fdd98abb181569f5fe6a9 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Sun, 4 Apr 2021 14:42:08 -0400 Subject: [PATCH] fix: index out of bounds when querying stored messages --- cmd/root.go | 6 ++++-- waku/v2/protocol/waku_store/waku_store.go | 12 +++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 2f6a9118..ad7f1ddd 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -186,8 +186,10 @@ var rootCmd = &cobra.Command{ fmt.Println(fmt.Sprint("Page Size: ", response.PagingInfo.PageSize)) fmt.Println(fmt.Sprint("Direction: ", response.PagingInfo.Direction)) - fmt.Println(fmt.Sprint("Cursor - ReceivedTime: ", response.PagingInfo.Cursor.ReceivedTime)) - fmt.Println(fmt.Sprint("Cursor - Digest: ", hex.EncodeToString(response.PagingInfo.Cursor.Digest))) + if response.PagingInfo.Cursor != nil { + fmt.Println(fmt.Sprint("Cursor - ReceivedTime: ", response.PagingInfo.Cursor.ReceivedTime)) + fmt.Println(fmt.Sprint("Cursor - Digest: ", hex.EncodeToString(response.PagingInfo.Cursor.Digest))) + } fmt.Println("Messages:") for i, msg := range response.Messages { fmt.Println(fmt.Sprint(i, "- ", string(msg.Payload))) // Normaly you'd have to decode these, but i'm using v0 diff --git a/waku/v2/protocol/waku_store/waku_store.go b/waku/v2/protocol/waku_store/waku_store.go index d0c5f6be..85ab9502 100644 --- a/waku/v2/protocol/waku_store/waku_store.go +++ b/waku/v2/protocol/waku_store/waku_store.go @@ -78,7 +78,7 @@ func paginateWithIndex(list []IndexedWakuMessage, pinfo *protocol.PagingInfo) (r } } - foundIndex := findIndex(&msgList, cursor) + foundIndex := findIndex(msgList, cursor) if foundIndex == -1 { // the cursor is not valid return nil, &protocol.PagingInfo{PageSize: 0, Cursor: pinfo.Cursor, Direction: pinfo.Direction} } @@ -95,6 +95,9 @@ func paginateWithIndex(list []IndexedWakuMessage, pinfo *protocol.PagingInfo) (r } s = foundIndex + 1 // non inclusive e = foundIndex + retrievedPageSize + if e < 0 { + e = 0 + } newCursor = msgList[e].index // the new cursor points to the end of the page case protocol.PagingInfo_BACKWARD: // backward pagination remainingMessages := foundIndex @@ -104,6 +107,9 @@ func paginateWithIndex(list []IndexedWakuMessage, pinfo *protocol.PagingInfo) (r foundIndex = foundIndex + 1 } s = foundIndex - retrievedPageSize + if s >= len(msgList) { + s = len(msgList) - 1 + } e = foundIndex - 1 newCursor = msgList[s].index // the new cursor points to the begining of the page } @@ -308,10 +314,10 @@ func indexedWakuMessageComparison(x, y IndexedWakuMessage) int { return indexComparison(x.index, y.index) } -func findIndex(msgList *[]IndexedWakuMessage, index *protocol.Index) int { +func findIndex(msgList []IndexedWakuMessage, index *protocol.Index) int { // returns the position of an IndexedWakuMessage in msgList whose index value matches the given index // returns -1 if no match is found - for i, indexedWakuMessage := range *msgList { + for i, indexedWakuMessage := range msgList { if bytes.Compare(indexedWakuMessage.index.Digest, index.Digest) == 0 && indexedWakuMessage.index.ReceivedTime == index.ReceivedTime { return i }