From 5acf01d9840b3a9b4147fbbede7293fe6cdd1a2c Mon Sep 17 00:00:00 2001 From: "diamondburned (Forefront)" Date: Fri, 7 Feb 2020 00:22:41 -0800 Subject: [PATCH] State: Fixed bug where range values are dereferenced incorrectly https://github.com/golang/go/wiki/CommonMistakes#using-reference-to-loop-iterator-variable --- state/state.go | 8 ++++++++ state/state_events.go | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/state/state.go b/state/state.go index b0c0bb7..5a5371d 100644 --- a/state/state.go +++ b/state/state.go @@ -193,6 +193,8 @@ func (s *State) Channels(guildID discord.Snowflake) ([]discord.Channel, error) { } for _, ch := range c { + ch := ch + if err := s.Store.ChannelSet(&ch); err != nil { return nil, err } @@ -272,6 +274,8 @@ func (s *State) Guilds() ([]discord.Guild, error) { } for _, ch := range c { + ch := ch + if err := s.Store.GuildSet(&ch); err != nil { return nil, err } @@ -444,6 +448,8 @@ func (s *State) Role( var role *discord.Role for _, r := range rs { + r := r + if r.ID == roleID { role = &r } @@ -468,6 +474,8 @@ func (s *State) Roles(guildID discord.Snowflake) ([]discord.Role, error) { } for _, r := range rs { + r := r + if err := s.RoleSet(guildID, &r); err != nil { return rs, err } diff --git a/state/state_events.go b/state/state_events.go index 34a893f..68757f4 100644 --- a/state/state_events.go +++ b/state/state_events.go @@ -24,6 +24,8 @@ func (s *State) onEvent(iface interface{}) { case *gateway.ReadyEvent: // Handle guilds for _, g := range ev.Guilds { + g := g + if err := s.Store.GuildSet(&g); err != nil { s.stateErr(err, "Failed to set guild in state") } @@ -31,6 +33,8 @@ func (s *State) onEvent(iface interface{}) { // Handle private channels for _, ch := range ev.PrivateChannels { + ch := ch + if err := s.Store.ChannelSet(&ch); err != nil { s.stateErr(err, "Failed to set channel in state") } @@ -50,12 +54,15 @@ func (s *State) onEvent(iface interface{}) { } for _, m := range ev.Members { + m := m + if err := s.Store.MemberSet(ev.Guild.ID, &m); err != nil { s.stateErr(err, "Failed to add a member from guild in state") } } for _, ch := range ev.Channels { + ch := ch ch.GuildID = ev.Guild.ID // just to make sure if err := s.Store.ChannelSet(&ch); err != nil { @@ -64,6 +71,8 @@ func (s *State) onEvent(iface interface{}) { } for _, p := range ev.Presences { + p := p + if err := s.Store.PresenceSet(ev.Guild.ID, &p); err != nil { s.stateErr(err, "Failed to add a presence from guild in state") } @@ -101,12 +110,16 @@ func (s *State) onEvent(iface interface{}) { case *gateway.GuildMembersChunkEvent: for _, m := range ev.Members { + m := m + if err := s.Store.MemberSet(ev.GuildID, &m); err != nil { s.stateErr(err, "Failed to add a member from chunk in state") } } for _, p := range ev.Presences { + p := p + if err := s.Store.PresenceSet(ev.GuildID, &p); err != nil { s.stateErr(err, "Failed to add a presence from chunk in state") }