From f0b6f207b31b8791cf42dacbffa7233becd3d73d Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sun, 24 Mar 2019 12:38:54 +0300 Subject: fixes #1897 (rare crash in IRC, when one user changes his nickname) --- src/mir_app/src/chat.h | 3 ++- src/mir_app/src/chat_manager.cpp | 30 +++++++++++++++++++++--------- src/mir_app/src/chat_svc.cpp | 4 +++- 3 files changed, 26 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/mir_app/src/chat.h b/src/mir_app/src/chat.h index c86cc9b16f..f947c2c46b 100644 --- a/src/mir_app/src/chat.h +++ b/src/mir_app/src/chat.h @@ -81,7 +81,8 @@ WORD TM_StringToWord(STATUSINFO *pStatusList, const wchar_t *pszStatus) BOOL UM_RemoveAll(SESSION_INFO *si); BOOL UM_SetStatusEx(SESSION_INFO *si, const wchar_t* pszText, int flags); -bool UM_SortUser(SESSION_INFO *si, const wchar_t *pszUID); +void UM_SortKeys(SESSION_INFO *si); +void UM_SortUser(SESSION_INFO *si); // clist.c BOOL AddEvent(MCONTACT hContact, HICON hIcon, MEVENT hEvent, int type, wchar_t* fmt, ...); diff --git a/src/mir_app/src/chat_manager.cpp b/src/mir_app/src/chat_manager.cpp index 2bb9b62b3f..9b8fcd4ced 100644 --- a/src/mir_app/src/chat_manager.cpp +++ b/src/mir_app/src/chat_manager.cpp @@ -281,7 +281,7 @@ BOOL SM_GiveStatus(const wchar_t *pszID, const char *pszModule, const wchar_t *p USERINFO *ui = g_chatApi.UM_GiveStatus(si, pszUID, TM_StringToWord(si->pStatuses, pszStatus)); if (ui) { - UM_SortUser(si, ui->pszUID); + UM_SortUser(si); if (si->pDlg) si->pDlg->UpdateNickList(); } @@ -296,7 +296,7 @@ BOOL SM_SetContactStatus(const wchar_t *pszID, const char *pszModule, const wcha USERINFO *ui = g_chatApi.UM_SetContactStatus(si, pszUID, wStatus); if (ui) { - UM_SortUser(si, ui->pszUID); + UM_SortUser(si); if (si->pDlg) si->pDlg->UpdateNickList(); } @@ -311,7 +311,7 @@ BOOL SM_TakeStatus(const wchar_t *pszID, const char *pszModule, const wchar_t *p USERINFO *ui = g_chatApi.UM_TakeStatus(si, pszUID, TM_StringToWord(si->pStatuses, pszStatus)); if (ui) { - UM_SortUser(si, ui->pszUID); + UM_SortUser(si); if (si->pDlg) si->pDlg->UpdateNickList(); } @@ -371,7 +371,7 @@ BOOL SM_ChangeNick(const wchar_t *pszID, const char *pszModule, GCEVENT *gce) USERINFO *ui = UM_FindUser(si, gce->pszUID.w); if (ui) { replaceStrW(ui->pszNick, gce->pszText.w); - UM_SortUser(si, ui->pszUID); + UM_SortUser(si); if (si->pDlg) si->pDlg->UpdateNickList(); if (g_chatApi.OnChangeNick) @@ -684,20 +684,32 @@ static USERINFO* UM_FindUser(SESSION_INFO *si, const wchar_t *pszUID) return si->getKeyList().find(&tmp); } +///////////////////////////////////////////////////////////////////////////////////////// + +static int compareKeysStub(const void *p1, const void *p2) +{ + return CompareKeys(*(USERINFO**)p1, *(USERINFO**)p2); +} + +void UM_SortKeys(SESSION_INFO *si) +{ + qsort(si->arKeys.getArray(), si->arKeys.getCount(), sizeof(void*), compareKeysStub); +} + +///////////////////////////////////////////////////////////////////////////////////////// + static int compareStub(const void *p1, const void *p2) { return CompareUser(*(USERINFO**)p1, *(USERINFO**)p2); } -bool UM_SortUser(SESSION_INFO *si, const wchar_t *pszUID) +void UM_SortUser(SESSION_INFO *si) { - if (!UM_FindUser(si, pszUID)) - return false; - qsort(si->arUsers.getArray(), si->arUsers.getCount(), sizeof(void*), compareStub); - return true; } +///////////////////////////////////////////////////////////////////////////////////////// + USERINFO* UM_AddUser(SESSION_INFO *si, const wchar_t *pszUID, const wchar_t *pszNick, WORD wStatus) { if (pszNick == nullptr) diff --git a/src/mir_app/src/chat_svc.cpp b/src/mir_app/src/chat_svc.cpp index dcffda8b06..5b98f4fe44 100644 --- a/src/mir_app/src/chat_svc.cpp +++ b/src/mir_app/src/chat_svc.cpp @@ -632,8 +632,10 @@ MIR_APP_DLL(int) Chat_ChangeUserId(const char *szModule, const wchar_t *wszId, c continue; USERINFO *ui = g_chatApi.UM_FindUser(si, wszOldId); - if (ui) + if (ui) { replaceStrW(ui->pszUID, wszNewId); + UM_SortKeys(si); + } if (wszId) break; } -- cgit v1.2.3