From 25a3cc3940db539a905b9c80fb526e3471c94cab Mon Sep 17 00:00:00 2001 From: texuf Date: Thu, 24 Oct 2024 15:52:33 -0700 Subject: [PATCH] Update the insertSorted function in snapshot.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I’m hunting for a bug where the membership gets screwy after a snapshot and asked chat gpt for a quck audit. It has some issues with this function even though i’m not aware of any specific issues here. --- core/node/events/snapshot.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/node/events/snapshot.go b/core/node/events/snapshot.go index 17d60b8b6e..2442d3712f 100644 --- a/core/node/events/snapshot.go +++ b/core/node/events/snapshot.go @@ -740,6 +740,11 @@ func findSorted[T any, K any](elements []*T, key K, cmp func(K, K) int, keyFn fu } func insertSorted[T any, K any](elements []*T, element *T, cmp func(K, K) int, keyFn func(*T) K) []*T { + length := len(elements) + if length == 0 { + elements = append(elements, element) + return elements + } index, found := slices.BinarySearchFunc(elements, keyFn(element), func(a *T, b K) int { return cmp(keyFn(a), b) }) @@ -747,6 +752,10 @@ func insertSorted[T any, K any](elements []*T, element *T, cmp func(K, K) int, k elements[index] = element return elements } + if index == length { + elements = append(elements, element) + return elements + } elements = append(elements, nil) copy(elements[index+1:], elements[index:]) elements[index] = element