Skip to content

Commit

Permalink
Fixed a potential race condition in the default state storage
Browse files Browse the repository at this point in the history
  • Loading branch information
diamondburned committed Jan 20, 2020
1 parent aa4154e commit 2cd9def
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
5 changes: 5 additions & 0 deletions state/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 6 additions & 6 deletions state/store_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -353,7 +353,7 @@ func (s *DefaultStore) Members(
return nil, ErrStoreNotFound
}

return ms, nil
return append([]discord.Member{}, ms...), nil
}

func (s *DefaultStore) MemberSet(
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -561,7 +561,7 @@ func (s *DefaultStore) Presences(
return nil, ErrStoreNotFound
}

return ps, nil
return append([]discord.Presence{}, ps...), nil
}

func (s *DefaultStore) PresenceSet(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 2cd9def

Please sign in to comment.