From 7badb4a37bc34e359ce651eded6511e21cb5683f Mon Sep 17 00:00:00 2001 From: Siddarth Kumar Date: Thu, 7 Sep 2023 06:36:43 +0000 Subject: [PATCH] Fix panic in peer manager (#714) * fix: slice error for index out of bounds this commit fixes slicing `notConnectedPeers` array with 0 to a negative value of `numPeersToConnect` by first checking if `numPeersToConnect` are greater than 0 --- waku/v2/peermanager/peer_manager.go | 4 ++++ waku/v2/peermanager/peer_manager_test.go | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/waku/v2/peermanager/peer_manager.go b/waku/v2/peermanager/peer_manager.go index e888c12a..5e99db7d 100644 --- a/waku/v2/peermanager/peer_manager.go +++ b/waku/v2/peermanager/peer_manager.go @@ -170,6 +170,10 @@ func (pm *PeerManager) connectToRelayPeers() { numPeersToConnect = notConnectedPeers.Len() - 1 } + if numPeersToConnect <= 0 { + return + } + pm.connectToPeers(notConnectedPeers[0:numPeersToConnect]) } //Else: Should we raise some sort of unhealthy event?? } diff --git a/waku/v2/peermanager/peer_manager_test.go b/waku/v2/peermanager/peer_manager_test.go index 15c2aedf..4f8bcbe1 100644 --- a/waku/v2/peermanager/peer_manager_test.go +++ b/waku/v2/peermanager/peer_manager_test.go @@ -151,3 +151,17 @@ func TestAdditionAndRemovalOfPeer(t *testing.T) { _, err = pm.SelectPeer(protocol2, nil, utils.Logger()) require.Error(t, err, utils.ErrNoPeersAvailable) } + +func TestConnectToRelayPeers(t *testing.T) { + defer func() { + if r := recover(); r != nil { + t.Errorf("TestConnectToRelayPeers panicked: %v", r) + } + }() + + _, pm, deferFn := initTest(t) + defer deferFn() + + pm.connectToRelayPeers() + +}