Fix addnext failing on an empty queue
Previously, addnext would try to unconditionally add at index one into a queue. This panicked if the queue was empty. Added two protections, one that checks the index in the InsertTrack function, the other in the addnext command itself to insert at zero if the queue is empty.
This commit is contained in:
parent
dff929ddc9
commit
caf2003d96
|
@ -85,6 +85,11 @@ func (q *Queue) InsertTrack(i int, t interfaces.Track) error {
|
||||||
q.mutex.Lock()
|
q.mutex.Lock()
|
||||||
beforeLen := len(q.Queue)
|
beforeLen := len(q.Queue)
|
||||||
|
|
||||||
|
if i < 0 || i > beforeLen {
|
||||||
|
q.mutex.Unlock()
|
||||||
|
return errors.New("Adding at invalid index in queue")
|
||||||
|
}
|
||||||
|
|
||||||
// An error should never occur here since maxTrackDuration is restricted to
|
// An error should never occur here since maxTrackDuration is restricted to
|
||||||
// ints. Any error in the configuration will be caught during yaml load.
|
// ints. Any error in the configuration will be caught during yaml load.
|
||||||
maxTrackDuration, _ := time.ParseDuration(fmt.Sprintf("%ds",
|
maxTrackDuration, _ := time.ParseDuration(fmt.Sprintf("%ds",
|
||||||
|
|
|
@ -75,7 +75,11 @@ func (c *AddNextCommand) Execute(user *gumble.User, args ...string) (string, boo
|
||||||
numAdded := 0
|
numAdded := 0
|
||||||
// We must loop backwards here to preserve the track order when inserting tracks.
|
// We must loop backwards here to preserve the track order when inserting tracks.
|
||||||
for i := len(allTracks) - 1; i >= 0; i-- {
|
for i := len(allTracks) - 1; i >= 0; i-- {
|
||||||
if err = DJ.Queue.InsertTrack(1, allTracks[i]); err != nil {
|
insertIndex := 1
|
||||||
|
if DJ.Queue.Length() == 0 {
|
||||||
|
insertIndex = 0
|
||||||
|
}
|
||||||
|
if err = DJ.Queue.InsertTrack(insertIndex, allTracks[i]); err != nil {
|
||||||
numTooLong++
|
numTooLong++
|
||||||
} else {
|
} else {
|
||||||
numAdded++
|
numAdded++
|
||||||
|
|
|
@ -6,3 +6,5 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package commands
|
||||||
|
|
||||||
|
// TODO: Add regression test for bug where addnext fails on an empty queue.
|
||||||
|
|
Reference in a new issue