From c9c613bc0131355019d900ea73c646deb6f127ac Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 2 Oct 2014 19:57:39 +0000 Subject: PluginUpdater: - fix against overflow in the interval's calculation; - code cleaning; git-svn-id: http://svn.miranda-ng.org/main/trunk@10670 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/PluginUpdater/src/DlgUpdate.cpp | 54 ++++++++++++++--------------- plugins/PluginUpdater/src/Notifications.cpp | 17 ++++----- plugins/PluginUpdater/src/Utils.cpp | 49 ++++++++++++++------------ plugins/PluginUpdater/src/Version.h | 2 +- 4 files changed, 63 insertions(+), 59 deletions(-) diff --git a/plugins/PluginUpdater/src/DlgUpdate.cpp b/plugins/PluginUpdater/src/DlgUpdate.cpp index d776ac0f31..18f75b3e50 100644 --- a/plugins/PluginUpdater/src/DlgUpdate.cpp +++ b/plugins/PluginUpdater/src/DlgUpdate.cpp @@ -603,7 +603,7 @@ static int ScanFolder(const TCHAR *tszFolder, size_t cbBaseLen, int level, const // Scan recursively all subfolders if (_tcscmp(ffd.cFileName, _T(".")) && _tcscmp(ffd.cFileName, _T(".."))) { mir_sntprintf(tszBuf, SIZEOF(tszBuf), _T("%s\\%s"), tszFolder, ffd.cFileName); - count += ScanFolder(tszBuf, cbBaseLen, level+1, tszBaseUrl, hashes, UpdateFiles); + count += ScanFolder(tszBuf, cbBaseLen, level + 1, tszBaseUrl, hashes, UpdateFiles); } } else if (isValidExtension(ffd.cFileName)) { @@ -613,7 +613,7 @@ static int ScanFolder(const TCHAR *tszFolder, size_t cbBaseLen, int level, const if (level == 0) _tcsncpy(tszNewName, ffd.cFileName, MAX_PATH); else - mir_sntprintf(tszNewName, SIZEOF(tszNewName), _T("%s\\%s"), tszFolder+cbBaseLen, ffd.cFileName); + mir_sntprintf(tszNewName, SIZEOF(tszNewName), _T("%s\\%s"), tszFolder + cbBaseLen, ffd.cFileName); } bool bHasNewVersion = true; @@ -632,12 +632,12 @@ static int ScanFolder(const TCHAR *tszFolder, size_t cbBaseLen, int level, const continue; // remove trailing w or W and try again - int iPos = int(p - tszNewName)-1; - strdel(p-1, 1); + int iPos = int(p - tszNewName) - 1; + strdel(p - 1, 1); if ((item = hashes.find((ServListEntry*)&pName)) == NULL) continue; - strdel(tszNewName+iPos, 1); + strdel(tszNewName + iPos, 1); } ptszUrl = item->m_name; @@ -649,7 +649,8 @@ static int ScanFolder(const TCHAR *tszFolder, size_t cbBaseLen, int level, const CalculateModuleHash(tszBuf, szMyHash); bHasNewVersion = strcmp(szMyHash, item->m_szHash) != 0; } - __except(EXCEPTION_EXECUTE_HANDLER) { + __except (EXCEPTION_EXECUTE_HANDLER) + { ZeroMemory(szMyHash, 0); // smth went wrong, reload a file from scratch } @@ -663,10 +664,10 @@ static int ScanFolder(const TCHAR *tszFolder, size_t cbBaseLen, int level, const // Compare versions if (bHasNewVersion) { // Yeah, we've got new version. - Netlib_LogfT(hNetlibUser,_T("Found update for %s"), tszBuf); + Netlib_LogfT(hNetlibUser, _T("Found update for %s"), tszBuf); FILEINFO *FileInfo = new FILEINFO; // copy the relative old name - _tcsncpy(FileInfo->tszOldName, tszBuf+cbBaseLen, SIZEOF(FileInfo->tszOldName)); + _tcsncpy(FileInfo->tszOldName, tszBuf + cbBaseLen, SIZEOF(FileInfo->tszOldName)); FileInfo->bDeleteOnly = bDeleteOnly; if (FileInfo->bDeleteOnly) { // save the full old name for deletion @@ -680,7 +681,7 @@ static int ScanFolder(const TCHAR *tszFolder, size_t cbBaseLen, int level, const TCHAR *p = _tcsrchr(tszBuf, '.'); if (p) *p = 0; p = _tcsrchr(tszBuf, '\\'); - p = (p) ? p+1 : tszBuf; + p = (p) ? p + 1 : tszBuf; _tcslwr(p); mir_sntprintf(FileInfo->File.tszDiskPath, SIZEOF(FileInfo->File.tszDiskPath), _T("%s\\Temp\\%s.zip"), tszRoot, p); @@ -707,20 +708,20 @@ static int ScanFolder(const TCHAR *tszFolder, size_t cbBaseLen, int level, const static void CheckUpdates(void *) { - Netlib_LogfT(hNetlibUser,_T("Checking for updates")); + Netlib_LogfT(hNetlibUser, _T("Checking for updates")); TCHAR tszTempPath[MAX_PATH]; DWORD dwLen = GetTempPath(SIZEOF(tszTempPath), tszTempPath); - if (tszTempPath[dwLen-1] == '\\') - tszTempPath[dwLen-1] = 0; + if (tszTempPath[dwLen - 1] == '\\') + tszTempPath[dwLen - 1] = 0; ptrT updateUrl(GetDefaultUrl()), baseUrl; - + SERVLIST hashes(50, CompareHashes); bool success = ParseHashes(updateUrl, baseUrl, hashes); if (success) { FILELIST *UpdateFiles = new FILELIST(20); - VARST dirname( _T("%miranda_path%")); - int count = ScanFolder(dirname, lstrlen(dirname)+1, 0, baseUrl, hashes, UpdateFiles); + VARST dirname(_T("%miranda_path%")); + int count = ScanFolder(dirname, lstrlen(dirname) + 1, 0, baseUrl, hashes, UpdateFiles); // Show dialog if (count == 0) { @@ -731,8 +732,8 @@ static void CheckUpdates(void *) else CallFunctionAsync(LaunchDialog, UpdateFiles); } - mir_forkthread(InitTimer, (success ? 0 :(void*) 2)); - + mir_forkthread(InitTimer, (success ? 0 : (void*)2)); + hashes.destroy(); hCheckThread = NULL; } @@ -745,12 +746,13 @@ void DoCheck(bool bSilent) ShowWindow(hwndDialog, SW_SHOW); SetForegroundWindow(hwndDialog); SetFocus(hwndDialog); - } else { + } + else { opts.bSilent = bSilent; #if MIRANDA_VER >= 0x0A00 db_set_dw(NULL, MODNAME, "LastUpdate", time(NULL)); #endif - hCheckThread = mir_forkthread(CheckUpdates, 0); + hCheckThread = mir_forkthread(CheckUpdates, 0); } } @@ -760,9 +762,9 @@ void UninitCheck() DestroyWindow(hwndDialog); } -INT_PTR MenuCommand(WPARAM,LPARAM) +INT_PTR MenuCommand(WPARAM, LPARAM) { - Netlib_LogfT(hNetlibUser,_T("Update started manually!")); + Netlib_LogfT(hNetlibUser, _T("Update started manually!")); DoCheck(false); return 0; } @@ -780,17 +782,15 @@ void UnloadCheck() void CheckUpdateOnStartup() { - if(opts.bUpdateOnStartup) - { - if (opts.bOnlyOnceADay) - { + if (opts.bUpdateOnStartup) { + if (opts.bOnlyOnceADay) { time_t now = time(NULL), was = db_get_dw(NULL, MODNAME, "LastUpdate", 0); if ((now - was) < 86400) return; } - Netlib_LogfT(hNetlibUser,_T("Update on startup started!")); + Netlib_LogfT(hNetlibUser, _T("Update on startup started!")); DoCheck(true); } -} \ No newline at end of file +} diff --git a/plugins/PluginUpdater/src/Notifications.cpp b/plugins/PluginUpdater/src/Notifications.cpp index 489baf750d..ac5ba40acd 100644 --- a/plugins/PluginUpdater/src/Notifications.cpp +++ b/plugins/PluginUpdater/src/Notifications.cpp @@ -68,12 +68,13 @@ static LRESULT CALLBACK PopupDlgProc(HWND hPopup, UINT uMsg, WPARAM wParam, LPAR return DefWindowProc(hPopup, uMsg, wParam, lParam); } -static void _stdcall RestartPrompt(void *) { +static void _stdcall RestartPrompt(void *) +{ TCHAR tszText[200]; mir_sntprintf(tszText, SIZEOF(tszText), _T("%s\n\n%s"), TranslateT("You need to restart your Miranda to apply installed updates."), TranslateT("Would you like to restart it now?")); if (MessageBox(0, tszText, TranslateT("Plugin Updater"), MB_YESNO | MB_ICONQUESTION | MB_TOPMOST) == IDYES) - CallService(MS_SYSTEM_RESTART, db_get_b(NULL,MODNAME,"RestartCurrentProfile",1) ? 1 : 0, 0); + CallService(MS_SYSTEM_RESTART, db_get_b(NULL, MODNAME, "RestartCurrentProfile", 1) ? 1 : 0, 0); } static LRESULT CALLBACK PopupDlgProcRestart(HWND hPopup, UINT uMsg, WPARAM wParam, LPARAM lParam) @@ -84,7 +85,7 @@ static LRESULT CALLBACK PopupDlgProcRestart(HWND hPopup, UINT uMsg, WPARAM wPara break; case WM_COMMAND: PUDeletePopup(hPopup); - CallFunctionAsync(RestartPrompt,0); + CallFunctionAsync(RestartPrompt, 0); break; } @@ -97,11 +98,11 @@ void ShowPopup(LPCTSTR ptszTitle, LPCTSTR ptszText, int Number) if (ServiceExists(MS_POPUP_ADDPOPUPT) && db_get_b(NULL, "Popup", "ModuleIsEnabled", 1)) { char setting[100]; mir_snprintf(setting, SIZEOF(setting), "Popups%d", Number); - if(db_get_b(NULL, MODNAME, setting, DEFAULT_POPUP_ENABLED)) { + if (db_get_b(NULL, MODNAME, setting, DEFAULT_POPUP_ENABLED)) { POPUPDATAT pd = { 0 }; pd.lchContact = NULL; pd.lchIcon = Skin_GetIcon("check_update"); - if(Number == POPUP_TYPE_MSG) { + if (Number == POPUP_TYPE_MSG) { pd.PluginWindowProc = PopupDlgProcRestart; pd.iSeconds = -1; } @@ -130,10 +131,10 @@ void ShowPopup(LPCTSTR ptszTitle, LPCTSTR ptszText, int Number) return; } } - - if(Number == POPUP_TYPE_ERROR) { + + if (Number == POPUP_TYPE_ERROR) { int iMsgType; - switch( Number ) { + switch (Number) { case POPUP_TYPE_MSG: iMsgType = MB_ICONSTOP; break; case POPUP_TYPE_ERROR: iMsgType = MB_ICONINFORMATION; break; case POPUP_TYPE_INFO: iMsgType = MB_ICONQUESTION; break; diff --git a/plugins/PluginUpdater/src/Utils.cpp b/plugins/PluginUpdater/src/Utils.cpp index 4154420bc5..1cda5a2f13 100644 --- a/plugins/PluginUpdater/src/Utils.cpp +++ b/plugins/PluginUpdater/src/Utils.cpp @@ -355,9 +355,9 @@ bool DownloadFile(FILEURL *pFileURL, HANDLE &nlc) ///////////////////////////////////////////////////////////////////////////////////////// -LONG PeriodToMilliseconds(const int period, BYTE &periodMeasure) +LONGLONG PeriodToMilliseconds(const int period, BYTE &periodMeasure) { - LONG result = period * 1000; + LONGLONG result = period * 1000; switch(periodMeasure) { case 1: // day @@ -381,11 +381,13 @@ void CALLBACK TimerAPCProc(void *, DWORD, DWORD) void InitTimer(void *type) { - if (opts.bUpdateOnPeriod) { - LONG interval = PeriodToMilliseconds(opts.Period, opts.bPeriodMeasure); + if (!opts.bUpdateOnPeriod) + return; - switch ((int)type) { - case 0: // default, plan next check relative to last check + LONGLONG interval = PeriodToMilliseconds(opts.Period, opts.bPeriodMeasure); + + switch ((int)type) { + case 0: // default, plan next check relative to last check { time_t now = time(NULL); time_t was = db_get_dw(NULL, MODNAME, "LastUpdate", 0); @@ -393,27 +395,28 @@ void InitTimer(void *type) interval -= (now - was) * 1000; if (interval <= 0) interval = 1000; // no last update or too far in the past -> do it now - break; - } - case 1: // options changed, use set interval from now - break; - case 2: // failed last check, check again in two hours - interval = 1000 * 60 * 60 * 2; - break; } + break; - FILETIME ft; - GetSystemTimeAsFileTime(&ft); - - LARGE_INTEGER li; - li.LowPart = ft.dwLowDateTime; - li.HighPart = ft.dwHighDateTime; - li.QuadPart += (ULONGLONG)(interval * 10000LL); - SetWaitableTimer(Timer, &li, 0, TimerAPCProc, NULL, 0); + case 1: // options changed, use set interval from now + break; - // Wait in an alertable state for the timer to go off. - SleepEx(INFINITE, TRUE); + case 2: // failed last check, check again in two hours + interval = 1000 * 60 * 60 * 2; + break; } + + FILETIME ft; + GetSystemTimeAsFileTime(&ft); + + LARGE_INTEGER li; + li.LowPart = ft.dwLowDateTime; + li.HighPart = ft.dwHighDateTime; + li.QuadPart += interval * 10000LL; + SetWaitableTimer(Timer, &li, 0, TimerAPCProc, NULL, 0); + + // Wait in an alertable state for the timer to go off. + SleepEx(INFINITE, TRUE); } void strdel(TCHAR *parBuffer, int len) diff --git a/plugins/PluginUpdater/src/Version.h b/plugins/PluginUpdater/src/Version.h index f498bab3d7..03e4af1f87 100644 --- a/plugins/PluginUpdater/src/Version.h +++ b/plugins/PluginUpdater/src/Version.h @@ -1,7 +1,7 @@ #define __MAJOR_VERSION 0 #define __MINOR_VERSION 1 #define __RELEASE_NUM 2 -#define __BUILD_NUM 3 +#define __BUILD_NUM 4 #include -- cgit v1.2.3