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
This commit is contained in:
Siddarth Kumar 2023-09-07 06:36:43 +00:00 committed by GitHub
parent 76b007163a
commit 7badb4a37b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View File

@ -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??
}

View File

@ -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()
}