Skip to content

Commit e8dfcac

Browse files
shoelzergitster
authored andcommitted
poll: use GetTickCount64() to avoid wrap-around issues
The value of timeout starts as an int value, and for this reason it cannot overflow unsigned long long aka ULONGLONG. The unsigned version of this initial value is available in orig_timeout. The difference (orig_timeout - elapsed) cannot wrap around because it is protected by a conditional (as can be seen in the patch text). Hence, the ULONGLONG difference can only have values that are smaller than the initial timeout value and truncation to int cannot overflow. Signed-off-by: Steve Hoelzer <[email protected]> [j6t: improved both implementation and log message] Signed-off-by: Johannes Sixt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4ede3d4 commit e8dfcac

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

compat/poll/poll.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
You should have received a copy of the GNU General Public License along
1919
with this program; if not, see <http://www.gnu.org/licenses/>. */
2020

21+
/* To bump the minimum Windows version to Windows Vista */
22+
#include "git-compat-util.h"
23+
2124
/* Tell gcc not to warn about the (nfd < 0) tests, below. */
2225
#if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__
2326
# pragma GCC diagnostic ignored "-Wtype-limits"
@@ -449,7 +452,8 @@ poll (struct pollfd *pfd, nfds_t nfd, int timeout)
449452
static HANDLE hEvent;
450453
WSANETWORKEVENTS ev;
451454
HANDLE h, handle_array[FD_SETSIZE + 2];
452-
DWORD ret, wait_timeout, nhandles, start = 0, elapsed, orig_timeout = 0;
455+
DWORD ret, wait_timeout, nhandles, orig_timeout = 0;
456+
ULONGLONG start = 0;
453457
fd_set rfds, wfds, xfds;
454458
BOOL poll_again;
455459
MSG msg;
@@ -465,7 +469,7 @@ poll (struct pollfd *pfd, nfds_t nfd, int timeout)
465469
if (timeout != INFTIM)
466470
{
467471
orig_timeout = timeout;
468-
start = GetTickCount();
472+
start = GetTickCount64();
469473
}
470474

471475
if (!hEvent)
@@ -614,8 +618,8 @@ poll (struct pollfd *pfd, nfds_t nfd, int timeout)
614618

615619
if (!rc && orig_timeout && timeout != INFTIM)
616620
{
617-
elapsed = GetTickCount() - start;
618-
timeout = elapsed >= orig_timeout ? 0 : orig_timeout - elapsed;
621+
ULONGLONG elapsed = GetTickCount64() - start;
622+
timeout = elapsed >= orig_timeout ? 0 : (int)(orig_timeout - elapsed);
619623
}
620624

621625
if (!rc && timeout)

0 commit comments

Comments
 (0)