From 6815dab807becb9e317a52a01240376d02ee8aa6 Mon Sep 17 00:00:00 2001 From: Lwrless Date: Tue, 8 Mar 2022 11:29:56 +0800 Subject: [PATCH] Fix panic caused by getting nonexistent participants. `Get` method of `Room` will return an error if the participant does not exist in the `Room`, thus setting `Publishing` on nil participant is prevented. --- main.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index e91ec43..4508cf0 100644 --- a/main.go +++ b/main.go @@ -71,17 +71,17 @@ func (v *Room) Add(p *Participant) error { return nil } -func (v *Room) Get(display string) *Participant { +func (v *Room) Get(display string) (*Participant, error) { v.lock.RLock() defer v.lock.RUnlock() for _, r := range v.Participants { if r.Display == display { - return r + return r, nil } } - return nil + return nil, errors.Errorf("Participant %v does not exist in room %v", display, v.Name) } func (v *Room) Remove(p *Participant) { @@ -279,7 +279,10 @@ func main() { } r, _ := rooms.LoadOrStore(obj.Message.Room, &Room{Name: obj.Message.Room}) - p := r.(*Room).Get(obj.Message.Display) + p, err := r.(*Room).Get(obj.Message.Display) + if err != nil { + return errors.Wrapf(err, "publish") + } // Now, the peer is publishing. p.Publishing = true @@ -299,7 +302,10 @@ func main() { } r, _ := rooms.LoadOrStore(obj.Message.Room, &Room{Name: obj.Message.Room}) - p := r.(*Room).Get(obj.Message.Display) + p, err := r.(*Room).Get(obj.Message.Display) + if err != nil { + return errors.Wrapf(err, "control") + } go r.(*Room).Notify(ctx, p, action.Message.Action, obj.Message.Call, obj.Message.Data) } else {