From 3ac7077263ebf6f236b8c51d8c549db49879b8ea Mon Sep 17 00:00:00 2001 From: Gabriel Plassard Date: Thu, 15 Oct 2015 00:07:50 +0200 Subject: [PATCH] Fix possible index out of range when auto shuffle is on --- songqueue.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/songqueue.go b/songqueue.go index f36e973..a00e08d 100644 --- a/songqueue.go +++ b/songqueue.go @@ -124,10 +124,12 @@ func (q *SongQueue) ShuffleSongs() { // Sets a random song as next song to be played // queueWasEmpty wether the queue was empty before adding the last song func (q *SongQueue) RandomNextSong(queueWasEmpty bool){ - nextSongIndex := 1 - if queueWasEmpty{ - nextSongIndex = 0 + if (q.Len() > 1){ + nextSongIndex := 1 + if queueWasEmpty{ + nextSongIndex = 0 + } + swapIndex := nextSongIndex + rand.Intn(q.Len() - 1) + q.queue[nextSongIndex], q.queue[swapIndex] = q.queue[swapIndex], q.queue[nextSongIndex] } - swapIndex := nextSongIndex + rand.Intn(q.Len()) - q.queue[nextSongIndex], q.queue[swapIndex] = q.queue[swapIndex], q.queue[nextSongIndex] }