From 2cd9def7785f4470a5f6768d04bac26b7be937c6 Mon Sep 17 00:00:00 2001 From: "diamondburned (Forefront)" Date: Mon, 20 Jan 2020 01:42:03 -0800 Subject: [PATCH] Fixed a potential race condition in the default state storage --- state/store.go | 5 +++++ state/store_default.go | 12 ++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/state/store.go b/state/store.go index 245ba00..298531e 100644 --- a/state/store.go +++ b/state/store.go @@ -16,6 +16,11 @@ type Store interface { // All methods in StoreGetter will be wrapped by the State. If the State can't // find anything in the storage, it will call the API itself and automatically // add what's missing into the storage. +// +// Methods that return with a slice should pay attention to race conditions that +// would mutate the underlying slice (and as a result the returned slice as +// well). The best way to avoid this is to copy the whole slice, like +// DefaultStore does. type StoreGetter interface { Self() (*discord.User, error) diff --git a/state/store_default.go b/state/store_default.go index 51c9fc4..27430b9 100644 --- a/state/store_default.go +++ b/state/store_default.go @@ -114,7 +114,7 @@ func (s *DefaultStore) Channels( return nil, ErrStoreNotFound } - return chs, nil + return append([]discord.Channel{}, chs...), nil } func (s *DefaultStore) PrivateChannels() ([]discord.Channel, error) { @@ -221,7 +221,7 @@ func (s *DefaultStore) Emojis( return nil, ErrStoreNotFound } - return gd.Emojis, nil + return append([]discord.Emoji{}, gd.Emojis...), nil } func (s *DefaultStore) EmojiSet( @@ -353,7 +353,7 @@ func (s *DefaultStore) Members( return nil, ErrStoreNotFound } - return ms, nil + return append([]discord.Member{}, ms...), nil } func (s *DefaultStore) MemberSet( @@ -437,7 +437,7 @@ func (s *DefaultStore) Messages( return nil, ErrStoreNotFound } - return ms, nil + return append([]discord.Message{}, ms...), nil } func (s *DefaultStore) MaxMessages() int { @@ -561,7 +561,7 @@ func (s *DefaultStore) Presences( return nil, ErrStoreNotFound } - return ps, nil + return append([]discord.Presence{}, ps...), nil } func (s *DefaultStore) PresenceSet( @@ -640,7 +640,7 @@ func (s *DefaultStore) Roles( return nil, ErrStoreNotFound } - return gd.Roles, nil + return append([]discord.Role{}, gd.Roles...), nil } func (s *DefaultStore) RoleSet(