Skip to content

Commit 81b93c8

Browse files
committed
Remove direct GetTickCount use
1 parent 6c343d9 commit 81b93c8

8 files changed

Lines changed: 34 additions & 19 deletions

File tree

Common/OS.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,12 @@ EXTERN_C bool IsUnicodeAvailableOS()
2424
}
2525
return (s_dwPlatformId != VER_PLATFORM_WIN32_WINDOWS);
2626
}
27+
28+
EXTERN_C DWORD MyGetTick32()
29+
{
30+
#pragma warning(push)
31+
#pragma warning(disable:28159)
32+
// If the caller ensures 32-bit value of tick, we should use GetTickCount safely
33+
return GetTickCount();
34+
#pragma warning(pop)
35+
}

Common/OS.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
#pragma once
88

99
EXTERN_C bool IsUnicodeAvailableOS();
10+
EXTERN_C DWORD MyGetTick32();

ShellDLL/FTPFldr.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "ShellDLL.h"
44
#include "FTPFldr.h"
55

6+
#include "OS.h"
7+
68
static void __stdcall GetFTPStatusMessage(int nStatus, LPCWSTR lpszMessage, CMyStringW& rstrMessage)
79
{
810
CMyStringW strMsg;
@@ -419,7 +421,7 @@ STDMETHODIMP CSFTPFolderFTP::ReadFile(HANDLE hFile, void* outBuffer, DWORD dwSiz
419421
auto* pSocket = data->pPassive->pPassive;
420422

421423
ULONG read = 0;
422-
DWORD dwStartTick = GetTickCount();
424+
DWORD dwStartTick = MyGetTick32();
423425
while (dwSize)
424426
{
425427
if (pSocket->IsRemoteClosed())
@@ -441,7 +443,7 @@ STDMETHODIMP CSFTPFolderFTP::ReadFile(HANDLE hFile, void* outBuffer, DWORD dwSiz
441443
if (!pSocket->CanReceive(0))
442444
{
443445
::Sleep(1);
444-
if (GetTickCount() - dwStartTick >= WAIT_RECEIVE_TIME)
446+
if (MyGetTick32() - dwStartTick >= WAIT_RECEIVE_TIME)
445447
break;
446448
continue;
447449
}
@@ -2567,7 +2569,7 @@ bool CSFTPFolderFTP::WaitForReceiveEstablishPassive(bool* pbWaiting, CFTPWaitEst
25672569

25682570
bool CSFTPFolderFTP::WaitForReceivePassive(bool* pbWaiting, CFTPWaitPassive* pPassive, DWORD dwTimeoutMilliseconds)
25692571
{
2570-
auto dwEnd = GetTickCount() + dwTimeoutMilliseconds;
2572+
auto dwEnd = MyGetTick32() + dwTimeoutMilliseconds;
25712573
while (*pbWaiting)
25722574
{
25732575
if (!pPassive->pPassive)
@@ -2628,7 +2630,7 @@ bool CSFTPFolderFTP::WaitForReceivePassive(bool* pbWaiting, CFTPWaitPassive* pPa
26282630
pPassive->nWaitFlags == CFTPWaitPassive::WaitFlags::Error)
26292631
break;
26302632
::Sleep(1);
2631-
if (dwTimeoutMilliseconds != INFINITE && static_cast<long>(GetTickCount()) - static_cast<long>(dwEnd) >= 0)
2633+
if (dwTimeoutMilliseconds != INFINITE && static_cast<long>(MyGetTick32()) - static_cast<long>(dwEnd) >= 0)
26322634
return false;
26332635
}
26342636
return true;

ShellDLL/SFTPStrm.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "ShellDLL.h"
99
#include "SFTPStrm.h"
1010

11+
#include "OS.h"
12+
1113
#define CACHE_SIZE 32768
1214

1315
////////////////////////////////////////////////////////////////////////////////
@@ -21,7 +23,7 @@ bool CSFTPSyncMessenger::WaitForCurrentMessage()
2123
if (!m_pChannel->RegisterMessageListener(uRegID, this))
2224
return false;
2325

24-
DWORD dwStart = GetTickCount();
26+
DWORD dwStart = MyGetTick32();
2527
while (true)
2628
{
2729
auto hr = m_pProcessor->PumpSocketAndMessage(0);
@@ -38,7 +40,7 @@ bool CSFTPSyncMessenger::WaitForCurrentMessage()
3840
bRet = !m_bFailed;
3941
break;
4042
}
41-
if (GetTickCount() - dwStart >= 10000)
43+
if (MyGetTick32() - dwStart >= 10000)
4244
{
4345
bRet = false;
4446
break;

ShellDLL/ShellDLL.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ bool CMainDLL::InitInstance()
12411241
// for SSL library
12421242
SSL_library_init();
12431243

1244-
::srand((unsigned int) (time(NULL) * GetTickCount()));
1244+
::srand(static_cast<unsigned int>(time(NULL)));
12451245

12461246
// Check whether we can use Unicode version of GetOpenFileName/GetSaveFileName
12471247
m_bUseOFNUnicode = IsUnicodeAvailableOS();
@@ -2328,7 +2328,7 @@ UINT_PTR CMainDLL::RegisterTimer(DWORD dwSpan, PFNEASYSFTPTIMERPROC pfnTimerProc
23282328
pData->pfnTimerProc = pfnTimerProc;
23292329
pData->lParam = lParam;
23302330
pData->dwSpan = dwSpan;
2331-
pData->dwTickStart = GetTickCount();
2331+
pData->dwTickStart = MyGetTick32();
23322332
::EnterCriticalSection(&theApp.m_csTimer);
23332333
m_arrTimers.Add(pData);
23342334
SetEvent(m_TimerThread.m_hEventChanged);
@@ -2547,7 +2547,7 @@ int CMainDLL::CTimerThread::Run()
25472547
lastCount = 0;
25482548
while (!m_bExit)
25492549
{
2550-
DWORD dwTick = GetTickCount();
2550+
DWORD dwTick = MyGetTick32();
25512551
CMyTimerData* pData = NULL;
25522552
::EnterCriticalSection(&theApp.m_csTimer);
25532553
auto newCount = theApp.m_arrTimers.GetCount();

ShellDLL/Transfer.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ CTransferDialog::CTransferItem* CTransferDialog::AddTransferItem(ULONGLONG uliMa
6868
pItem->strDestName = lpszDestName;
6969
if (lpszLocalFileName)
7070
pItem->strLocalFileName = lpszLocalFileName;
71-
pItem->dwStartTime = pItem->dwCurrentTime = GetTickCount();
71+
pItem->dwStartTime = pItem->dwCurrentTime = GetTickCount64();
7272
pItem->bWaiting = bWaiting;
7373
pItem->bFinished = false;
7474
pItem->bCanceled = false;
@@ -104,7 +104,7 @@ void CTransferDialog::SetTransferItemSize(CTransferItem* pvItem, ULONGLONG uliMa
104104
}
105105
pvItem->uliMax = uliMax;
106106
pvItem->uliCurrent = 0;
107-
pvItem->dwCurrentTime = GetTickCount();
107+
pvItem->dwCurrentTime = GetTickCount64();
108108
if (pvItem->bWaiting)
109109
{
110110
pvItem->bWaiting = false;
@@ -128,7 +128,7 @@ void CTransferDialog::UpdateTransferItem(CTransferItem* pvItem, ULONGLONG uliPos
128128
return;
129129
}
130130
pvItem->uliCurrent = uliPosition;
131-
pvItem->dwCurrentTime = GetTickCount();
131+
pvItem->dwCurrentTime = GetTickCount64();
132132
if (pvItem->bWaiting)
133133
{
134134
pvItem->bWaiting = false;
@@ -369,7 +369,7 @@ LRESULT CTransferDialog::OnDrawItem(WPARAM wParam, LPARAM lParam)
369369
else
370370
{
371371
CMyStringW strSizeCur, strSizeRate;
372-
DWORD dwPassTime = pItem->dwCurrentTime - pItem->dwStartTime;
372+
auto dwPassTime = pItem->dwCurrentTime - pItem->dwStartTime;
373373
{
374374
ULARGE_INTEGER uli;
375375
uli.QuadPart = pItem->uliCurrent;
@@ -392,14 +392,14 @@ LRESULT CTransferDialog::OnDrawItem(WPARAM wParam, LPARAM lParam)
392392
uli.QuadPart = 1024;
393393
uli.QuadPart = ((pItem->uliMax - pItem->uliCurrent) * 1000 / uli.QuadPart);
394394
if (uli.HighPart != 0)
395-
dwPassTime = 0xFFFFFFFF;
395+
dwPassTime = 0xFFFFFFFFFFFFFFFF;
396396
else
397397
dwPassTime = uli.LowPart;
398398
}
399399
{
400400
register int nMinute, nSecond;
401401
nMinute = (int) (dwPassTime / 60000);
402-
nSecond = (int) ((dwPassTime - ((DWORD) nMinute * 60000)) / 1000);
402+
nSecond = (int) ((dwPassTime - (static_cast<decltype(dwPassTime)>(nMinute) * 60000)) / 1000);
403403
strTransfer.Format(IDS_TRANSFER_RATE, (LPCWSTR) strSizeCur, (LPCWSTR) strSizeMax,
404404
nMinute, nSecond, (LPCWSTR) strSizeRate);
405405
}

ShellDLL/Transfer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ class CTransferDialog :
2828
{
2929
ULONGLONG uliCurrent;
3030
ULONGLONG uliMax;
31-
DWORD dwStartTime;
32-
DWORD dwCurrentTime;
31+
ULONGLONG dwStartTime;
32+
ULONGLONG dwCurrentTime;
3333
CMyStringW strDestName;
3434
CMyStringW strLocalFileName;
3535
int iIconIndex;

ShellDLL/WinOpSSH.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "WinOpSSH.h"
44

55
#include <AclAPI.h>
6+
#include "OS.h"
67

78
// ConvertEndian
89
#include "ExBuffer.h"
@@ -111,7 +112,7 @@ bool CWinOpenSSHAgent::Query(const void* dataSend, size_t dataSendSize, void** d
111112
}
112113
}
113114

114-
DWORD dwStartTick = ::GetTickCount() + 1000;
115+
DWORD dwStartTick = MyGetTick32() + 1000;
115116
while (true)
116117
{
117118
h = ::CreateFile(WIN_OPENSSH_AGENT_PIPE, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
@@ -126,7 +127,7 @@ bool CWinOpenSSHAgent::Query(const void* dataSend, size_t dataSendSize, void** d
126127
LogWin32LastError(L"CreateFile");
127128
return false;
128129
}
129-
if (::GetTickCount() >= dwStartTick)
130+
if (MyGetTick32() >= dwStartTick)
130131
{
131132
return false;
132133
}

0 commit comments

Comments
 (0)