Skip to content

Commit eaf190b

Browse files
committed
Merge pull request git-for-windows#1003 from shoelzer/master
poll: Use GetTickCount64 to avoid wraparound issues
2 parents 76a2915 + accec9c commit eaf190b

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

compat/poll/poll.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,20 @@ win32_compute_revents_socket (SOCKET h, int sought, long lNetworkEvents)
266266
return happened;
267267
}
268268

269+
#include <windows.h>
270+
#include "compat/win32/lazyload.h"
271+
272+
static ULONGLONG CompatGetTickCount64(void)
273+
{
274+
DECLARE_PROC_ADDR(kernel32.dll, ULONGLONG, GetTickCount64, void);
275+
276+
if (!INIT_PROC_ADDR(GetTickCount64))
277+
return (ULONGLONG)GetTickCount();
278+
279+
return GetTickCount64();
280+
}
281+
#define GetTickCount64 CompatGetTickCount64
282+
269283
#else /* !MinGW */
270284

271285
/* Convert select(2) returned fd_sets into poll(2) revents values. */
@@ -449,7 +463,8 @@ poll (struct pollfd *pfd, nfds_t nfd, int timeout)
449463
static HANDLE hEvent;
450464
WSANETWORKEVENTS ev;
451465
HANDLE h, handle_array[FD_SETSIZE + 2];
452-
DWORD ret, wait_timeout, nhandles, start = 0, elapsed, orig_timeout = 0;
466+
DWORD ret, wait_timeout, nhandles, elapsed, orig_timeout = 0;
467+
ULONGLONG start = 0;
453468
fd_set rfds, wfds, xfds;
454469
BOOL poll_again;
455470
MSG msg;
@@ -465,7 +480,7 @@ poll (struct pollfd *pfd, nfds_t nfd, int timeout)
465480
if (timeout != INFTIM)
466481
{
467482
orig_timeout = timeout;
468-
start = GetTickCount();
483+
start = GetTickCount64();
469484
}
470485

471486
if (!hEvent)
@@ -614,7 +629,7 @@ poll (struct pollfd *pfd, nfds_t nfd, int timeout)
614629

615630
if (!rc && orig_timeout && timeout != INFTIM)
616631
{
617-
elapsed = GetTickCount() - start;
632+
elapsed = (DWORD)(GetTickCount64() - start);
618633
timeout = elapsed >= orig_timeout ? 0 : orig_timeout - elapsed;
619634
}
620635

0 commit comments

Comments
 (0)