From bca86ffd8fd66e71ca873e160d7bb4d35b7830d5 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 6 Jun 2019 20:15:07 +0300 Subject: Sessions: fix for a memory corruption issue --- plugins/Sessions/Src/Main.cpp | 2 +- plugins/Sessions/Src/Utils.cpp | 180 +++++++++++++---------------------------- plugins/Sessions/Src/Utils.h | 24 +++--- plugins/Sessions/Src/stdafx.h | 2 +- plugins/Sessions/Src/version.h | 4 +- 5 files changed, 70 insertions(+), 142 deletions(-) diff --git a/plugins/Sessions/Src/Main.cpp b/plugins/Sessions/Src/Main.cpp index 56d3d7bc6d..8a5ca2565b 100644 --- a/plugins/Sessions/Src/Main.cpp +++ b/plugins/Sessions/Src/Main.cpp @@ -28,7 +28,7 @@ bool g_hghostw; HWND hClistControl; int g_ses_limit; -size_t g_ses_count; +int g_ses_count; bool g_bExclHidden; bool g_bWarnOnHidden; bool g_bOtherWarnings; diff --git a/plugins/Sessions/Src/Utils.cpp b/plugins/Sessions/Src/Utils.cpp index de6a05d92b..6a5cf602b5 100644 --- a/plugins/Sessions/Src/Utils.cpp +++ b/plugins/Sessions/Src/Utils.cpp @@ -22,89 +22,50 @@ along with this program. If not, see . void AddSessionMark(MCONTACT hContact, int mode, char bit) { if (mode == 0) { - ptrA szValue(g_plugin.getStringA(hContact, "LastSessionsMarks")); - if (szValue) { - char temp_1 = szValue[0]; - for (int i = 0; i < g_ses_limit; i++) { - char temp_2 = szValue[i + 1]; - szValue[i + 1] = temp_1; - temp_1 = temp_2; - } - for (int i = g_ses_limit; i < 10; i++) - szValue[i] = '0'; - szValue[0] = bit; - g_plugin.setString(hContact, "LastSessionsMarks", szValue); - } - else if (bit == '1') - g_plugin.setString(hContact, "LastSessionsMarks", "10000000000"); + CMStringA szValue(g_plugin.getMStringA(hContact, "LastSessionsMarks")); + szValue.Insert(0, bit); + szValue.Truncate(g_ses_limit); + g_plugin.setString(hContact, "LastSessionsMarks", szValue); } else if (mode == 1) { - ptrA szValue(g_plugin.getStringA(hContact, "UserSessionsMarks")); - if (szValue) { - char *pszBuffer; - if (mir_strlen(szValue) < g_ses_count) { - pszBuffer = (char*)mir_alloc(g_ses_count + 1); - memset(pszBuffer, 0, (g_ses_count + 1)); - mir_strcpy(pszBuffer, szValue); - } - else pszBuffer = szValue.detach(); - - char temp_1 = pszBuffer[0]; - for (size_t i = 0; i < g_ses_count; i++) { - char temp_2 = pszBuffer[i + 1]; - pszBuffer[i + 1] = temp_1; - temp_1 = temp_2; - } - pszBuffer[0] = bit; - g_plugin.setString(hContact, "UserSessionsMarks", pszBuffer); - - mir_free(pszBuffer); - } - else if (bit == '1') - g_plugin.setString(hContact, "UserSessionsMarks", "10000000000"); - else - g_plugin.setString(hContact, "UserSessionsMarks", "00000000000"); + CMStringA szValue(g_plugin.getMStringA(hContact, "UserSessionsMarks")); + szValue.Insert(0, bit); + szValue.Truncate((int)g_ses_count); + g_plugin.setString(hContact, "UserSessionsMarks", szValue); } } void RemoveSessionMark(MCONTACT hContact, int mode, int marknum) { if (mode == 0) { - ptrA szValue(g_plugin.getStringA(hContact, "LastSessionsMarks")); - if (szValue) { - for (int i = marknum; i < g_ses_limit; i++) - szValue[i] = szValue[i + 1]; - - for (int i = g_ses_limit; i < 10; i++) - szValue[i] = '0'; - + CMStringA szValue(g_plugin.getMStringA(hContact, "LastSessionsMarks")); + if (!szValue.IsEmpty() && marknum < szValue.GetLength()) { + szValue.Delete(marknum, 1); g_plugin.setString(hContact, "LastSessionsMarks", szValue); } } else if (mode == 1) { - ptrA szValue(g_plugin.getStringA(hContact, "UserSessionsMarks")); - if (szValue) { - for (int i = marknum; i < g_ses_limit; i++) - szValue[i] = szValue[i + 1]; - + CMStringA szValue(g_plugin.getMStringA(hContact, "UserSessionsMarks")); + if (!szValue.IsEmpty() && marknum < szValue.GetLength()) { + szValue.Delete(marknum, 1); g_plugin.setString(hContact, "UserSessionsMarks", szValue); } } } -void SetSessionMark(MCONTACT hContact, int mode, char bit, unsigned int marknum) +void SetSessionMark(MCONTACT hContact, int mode, char bit, int marknum) { if (mode == 0) { - ptrA szValue(g_plugin.getStringA(hContact, "LastSessionsMarks")); - if (szValue) { - szValue[marknum] = bit; + CMStringA szValue(g_plugin.getMStringA(hContact, "LastSessionsMarks")); + if (!szValue.IsEmpty() && marknum < szValue.GetLength()) { + szValue.SetAt(marknum, bit); g_plugin.setString(hContact, "LastSessionsMarks", szValue); } } else if (mode == 1) { - ptrA szValue(g_plugin.getStringA(hContact, "UserSessionsMarks")); - if (szValue) { - szValue[marknum] = bit; + CMStringA szValue(g_plugin.getMStringA(hContact, "UserSessionsMarks")); + if (!szValue.IsEmpty() && marknum < szValue.GetLength()) { + szValue.SetAt(marknum, bit); g_plugin.setString(hContact, "UserSessionsMarks", szValue); } } @@ -112,13 +73,13 @@ void SetSessionMark(MCONTACT hContact, int mode, char bit, unsigned int marknum) bool LoadContactsFromMask(MCONTACT hContact, int mode, int count) { - ptrA szValue; + CMStringA szValue; if (mode == 0) - szValue = g_plugin.getStringA(hContact, "LastSessionsMarks"); + szValue = g_plugin.getMStringA(hContact, "LastSessionsMarks"); else if (mode == 1) - szValue = g_plugin.getStringA(hContact, "UserSessionsMarks"); + szValue = g_plugin.getMStringA(hContact, "UserSessionsMarks"); - if (szValue == NULL) + if (szValue.IsEmpty()) return false; return szValue[count] == '1'; @@ -126,97 +87,64 @@ bool LoadContactsFromMask(MCONTACT hContact, int mode, int count) void AddInSessionOrder(MCONTACT hContact, int mode, int ordernum, int writemode) { - char szFormNumBuf[100]; - mir_snprintf(szFormNumBuf, "%02u", ordernum); + char buf[100]; + mir_snprintf(buf, "%02u", ordernum); if (mode == 0) { - ptrA szValue(g_plugin.getStringA(hContact, "LastSessionsMarks")); - if (szValue) { - int len = (int)mir_strlen(szValue); - if (!len) - len = 20; - - char *temp2 = (char*)_alloca(len - 1); - strncpy(temp2, szValue, len - 2); - temp2[len - 2] = '\0'; - - char *temp = (char*)_alloca(len + 1); - mir_snprintf(temp, len + 1, "%02u%s", ordernum, temp2); - - for (int i = (g_ses_limit * 2); i < 20; i++) - temp[i] = '0'; - - g_plugin.setString(hContact, "LastSessionsOrder", temp); - } - else if (writemode == 1) { - mir_snprintf(szFormNumBuf, "%02u%s", ordernum, "000000000000000000"); - g_plugin.setString(hContact, "LastSessionsOrder", szFormNumBuf); - } + CMStringA szValue(g_plugin.getMStringA(hContact, "LastSessionsOrder")); + szValue.Insert(0, buf); + szValue.Truncate(g_ses_limit * 2); + g_plugin.setString(hContact, "LastSessionsOrder", szValue); } else if (mode == 1) { - ptrA szValue(g_plugin.getStringA(hContact, "UserSessionsOrder")); - if (szValue) { - char *pszBuffer; - if (mir_strlen(szValue) < (g_ses_count * 2)) { - pszBuffer = (char*)mir_alloc((g_ses_count * 2) + 1); - memset(pszBuffer, 0, ((g_ses_count * 2) + 1)); - mir_strcpy(pszBuffer, szValue); - } - else pszBuffer = mir_strdup(szValue); - - int len = (int)mir_strlen(pszBuffer); - len = (len == 0) ? 20 : len + 2; - char *temp = (char*)_alloca(len + 1); - mir_snprintf(temp, len + 1, "%02u%s", ordernum, szValue); - - g_plugin.setString(hContact, "UserSessionsOrder", temp); - mir_free(pszBuffer); - } - else if (writemode == 1) - g_plugin.setString(hContact, "UserSessionsOrder", szFormNumBuf); - else - g_plugin.setString(hContact, "UserSessionsOrder", "00"); + CMStringA szValue(g_plugin.getMStringA(hContact, "UserSessionsOrder")); + szValue.Insert(0, buf); + szValue.Truncate(g_ses_count * 2); + g_plugin.setString(hContact, "UserSessionsOrder", szValue); } } int GetInSessionOrder(MCONTACT hContact, int mode, int count) { - char szTemp[3]; + char szTemp[3] = { 0, 0, 0 }; + count *= 2; + if (mode == 0) { - ptrA szValue(g_plugin.getStringA(hContact, "LastSessionsOrder")); - if (szValue) { - strncpy_s(szTemp, &szValue[count * 2], 2); + CMStringA szValue(g_plugin.getMStringA(hContact, "LastSessionsOrder")); + if (!szValue.IsEmpty() && count < szValue.GetLength()) { + memcpy(szTemp, szValue.c_str() + count, 2); return atoi(szTemp); } } else if (mode == 1) { - ptrA szValue(g_plugin.getStringA(hContact, "UserSessionsOrder")); - if (szValue) { - strncpy_s(szTemp, &szValue[count * 2], 2); + CMStringA szValue(g_plugin.getMStringA(hContact, "UserSessionsOrder")); + if (!szValue.IsEmpty() && count < szValue.GetLength()) { + memcpy(szTemp, szValue.c_str() + count, 2); return atoi(szTemp); } } return 0; } -void SetInSessionOrder(MCONTACT hContact, int mode, int count, unsigned int ordernum) +void SetInSessionOrder(MCONTACT hContact, int mode, int count, int ordernum) { char szTemp[3]; mir_snprintf(szTemp, "%02u", ordernum); + count *= 2; if (mode == 0) { - ptrA szValue(g_plugin.getStringA(hContact, "LastSessionsOrder")); - if (szValue) { - szValue[count * 2] = szTemp[0]; - szValue[count * 2 + 1] = szTemp[1]; + CMStringA szValue(g_plugin.getMStringA(hContact, "LastSessionsOrder")); + if (!szValue.IsEmpty() && count < szValue.GetLength()) { + szValue.SetAt(count, szTemp[0]); + szValue.SetAt(count + 1, szTemp[1]); g_plugin.setString(hContact, "LastSessionsOrder", szValue); } } else if (mode == 1) { - ptrA szValue(g_plugin.getStringA(hContact, "UserSessionsOrder")); - if (szValue) { - szValue[count * 2] = szTemp[0]; - szValue[count * 2 + 1] = szTemp[1]; + CMStringA szValue(g_plugin.getMStringA(hContact, "UserSessionsOrder")); + if (!szValue.IsEmpty() && count < szValue.GetLength()) { + szValue.SetAt(count, szTemp[0]); + szValue.SetAt(count + 1, szTemp[1]); g_plugin.setString(hContact, "UserSessionsOrder", szValue); } } diff --git a/plugins/Sessions/Src/Utils.h b/plugins/Sessions/Src/Utils.h index c474393433..ca43ddf913 100644 --- a/plugins/Sessions/Src/Utils.h +++ b/plugins/Sessions/Src/Utils.h @@ -20,25 +20,25 @@ along with this program. If not, see . #ifndef __UTILS_H__ # define __UTILS_H__ -void SetInSessionOrder(MCONTACT hContact,int mode,int count,unsigned int ordernum); -void AddInSessionOrder(MCONTACT hContact,int mode,int ordernum,int writemode); -int GetInSessionOrder(MCONTACT hContact,int mode,int count); -void AddSessionMark(MCONTACT hContact,int mode,char bit); -void RemoveSessionMark(MCONTACT hContact,int mode,int marknum); -void SetSessionMark(MCONTACT hContact,int mode,char bit,unsigned int marknum); +void SetInSessionOrder(MCONTACT hContact, int mode, int count, int ordernum); +void AddInSessionOrder(MCONTACT hContact, int mode, int ordernum, int writemode); +int GetInSessionOrder(MCONTACT hContact, int mode, int count); +void AddSessionMark(MCONTACT hContact, int mode, char bit); +void RemoveSessionMark(MCONTACT hContact, int mode, int marknum); +void SetSessionMark(MCONTACT hContact, int mode, char bit, int marknum); bool LoadContactsFromMask(MCONTACT hContact, int mode, int count); int AddToCurSession(MCONTACT hContact, LPARAM lparam); -int DelFromCurSession(MCONTACT hContact,LPARAM lparam); +int DelFromCurSession(MCONTACT hContact, LPARAM lparam); int CheckForDuplicate(MCONTACT contact_list[], MCONTACT lparam); -BOOL ResaveSettings(char* szName,int iFirst,int iLimit,wchar_t* pszPrevSetting); +BOOL ResaveSettings(char *szName, int iFirst, int iLimit, wchar_t *pszPrevSetting); void OffsetWindow(HWND parent, HWND hwnd, int dx, int dy); -int LoadSessionToCombobox (HWND hdlg,BOOL mode,int iLimit,char* pszSetting,int iFirstNum); -int MarkUserDefSession(int ses_count,BYTE bCheck); +int LoadSessionToCombobox(HWND hdlg, BOOL mode, int iLimit, char *pszSetting, int iFirstNum); +int MarkUserDefSession(int ses_count, BYTE bCheck); BYTE IsMarkedUserDefSession(int ses_count); void SavePosition(HWND hWnd, char *wndName); void LoadPosition(HWND hWnd, char *wndName); int CheckContactVisibility(MCONTACT hContact); -void RenameUserDefSession(int ses_count,wchar_t* ptszNewName); -int FillFavoritesMenu (HMENU hMenu,int iLimit); +void RenameUserDefSession(int ses_count, wchar_t *ptszNewName); +int FillFavoritesMenu(HMENU hMenu, int iLimit); #endif // __UTILS_H__ \ No newline at end of file diff --git a/plugins/Sessions/Src/stdafx.h b/plugins/Sessions/Src/stdafx.h index c6185f5acc..2d319c94f9 100644 --- a/plugins/Sessions/Src/stdafx.h +++ b/plugins/Sessions/Src/stdafx.h @@ -70,7 +70,7 @@ int SaveSessionDate(); extern MCONTACT session_list_recovered[255]; extern MCONTACT session_list[255]; extern int g_ses_limit; -extern size_t g_ses_count; +extern int g_ses_count; extern bool g_bExclHidden; extern bool g_bWarnOnHidden; extern bool g_bOtherWarnings; diff --git a/plugins/Sessions/Src/version.h b/plugins/Sessions/Src/version.h index 6900e6a108..53300b236f 100644 --- a/plugins/Sessions/Src/version.h +++ b/plugins/Sessions/Src/version.h @@ -1,7 +1,7 @@ #define __MAJOR_VERSION 0 #define __MINOR_VERSION 1 -#define __RELEASE_NUM 3 -#define __BUILD_NUM 2 +#define __RELEASE_NUM 4 +#define __BUILD_NUM 0 #include -- cgit v1.2.3