From 94843667844e72212560a0fa762862f9a9880f33 Mon Sep 17 00:00:00 2001 From: Claudio Costa Date: Mon, 26 Oct 2020 15:19:14 +0100 Subject: [PATCH] Fix nil dereference panic in SearchArchivedInTeam (#16064) Co-authored-by: Mattermod --- store/sqlstore/channel_store.go | 12 ++++++++---- store/storetest/channel_store.go | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/store/sqlstore/channel_store.go b/store/sqlstore/channel_store.go index 787e5d62c7..b281ec52e4 100644 --- a/store/sqlstore/channel_store.go +++ b/store/sqlstore/channel_store.go @@ -2544,15 +2544,19 @@ func (s SqlChannelStore) SearchArchivedInTeam(teamId string, term string, userId "UserId": userId, }) - output := *publicChannels - output = append(output, *privateChannels...) - outputErr := publicErr if privateErr != nil { outputErr = privateErr } - return &output, outputErr + if outputErr != nil { + return nil, outputErr + } + + output := *publicChannels + output = append(output, *privateChannels...) + + return &output, nil } func (s SqlChannelStore) SearchForUserInTeam(userId string, teamId string, term string, includeDeleted bool) (*model.ChannelList, error) { diff --git a/store/storetest/channel_store.go b/store/storetest/channel_store.go index 6cfae89c85..55e6178ab9 100644 --- a/store/storetest/channel_store.go +++ b/store/storetest/channel_store.go @@ -80,6 +80,7 @@ func TestChannelStore(t *testing.T, ss store.Store, s SqlSupplier) { t.Run("GetGuestCount", func(t *testing.T) { testGetGuestCount(t, ss) }) t.Run("SearchMore", func(t *testing.T) { testChannelStoreSearchMore(t, ss) }) t.Run("SearchInTeam", func(t *testing.T) { testChannelStoreSearchInTeam(t, ss, s) }) + t.Run("SearchArchivedInTeam", func(t *testing.T) { testChannelStoreSearchArchivedInTeam(t, ss, s) }) t.Run("SearchForUserInTeam", func(t *testing.T) { testChannelStoreSearchForUserInTeam(t, ss) }) t.Run("SearchAllChannels", func(t *testing.T) { testChannelStoreSearchAllChannels(t, ss) }) t.Run("GetMembersByIds", func(t *testing.T) { testChannelStoreGetMembersByIds(t, ss) }) @@ -4947,6 +4948,28 @@ func (s ByChannelDisplayName) Less(i, j int) bool { return s[i].Id < s[j].Id } +func testChannelStoreSearchArchivedInTeam(t *testing.T, ss store.Store, s SqlSupplier) { + teamId := model.NewId() + userId := model.NewId() + + t.Run("empty result", func(t *testing.T) { + list, err := ss.Channel().SearchArchivedInTeam(teamId, "term", userId) + require.Nil(t, err) + require.NotNil(t, list) + require.Empty(t, list) + }) + + t.Run("error", func(t *testing.T) { + // trigger a SQL error + s.GetMaster().Exec("ALTER TABLE Channels RENAME TO Channels_renamed") + defer s.GetMaster().Exec("ALTER TABLE Channels_renamed RENAME TO Channels") + + list, err := ss.Channel().SearchArchivedInTeam(teamId, "term", userId) + require.NotNil(t, err) + require.Nil(t, list) + }) +} + func testChannelStoreSearchInTeam(t *testing.T, ss store.Store, s SqlSupplier) { teamId := model.NewId() otherTeamId := model.NewId()