git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/1] poll: use GetTickCount64() to avoid wrap-around issues

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 4, 2018, 23:26 UTC
Message-ID
<xmqqefc0mnlh.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<46aa1893-095b-9f0c-4989-e63ebaa88705@kdbg.org>
Johannes Sixt <j6t@kdbg.org> writes:
Show 11 quoted lines
> Am 03.11.18 um 09:14 schrieb Carlo Arenas:
>> On Fri, Nov 2, 2018 at 9:44 AM Johannes Sixt <j6t@kdbg.org> wrote:
>>>
>>> +      timeout = elapsed >= orig_timeout ? 0 : (int)(orig_timeout - elapsed);
>>
>> nitpick: cast to DWORD instead of int
>
> No; timeout is of type int; after an explicit type cast we don't want
> to have another implicit conversion.
>
> -- Hannes

OK, thanks. It seems that the relative silence after this message is a sign that the resulting patch after squashing is what everybody is happey with?

-- >8 --
From: Steve Hoelzer <shoelzer@gmail.com>
Date: Wed, 31 Oct 2018 14:11:36 -0700
Subject: [PATCH] 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: Johannes Sixt <j6t@kdbg.org>
Acked-by: Steve Hoelzer <shoelzer@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 compat/poll/poll.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/compat/poll/poll.c b/compat/poll/poll.c
index ad5dcde439..4459408c7d 100644
--- a/compat/poll/poll.c
+++ b/compat/poll/poll.c
@@ -18,6 +18,9 @@
    You should have received a copy of the GNU General Public License along
    with this program; if not, see <http://www.gnu.org/licenses/>.  */
 
+/* To bump the minimum Windows version to Windows Vista */
+#include "git-compat-util.h"
+
 /* Tell gcc not to warn about the (nfd < 0) tests, below.  */
 #if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__
 # pragma GCC diagnostic ignored "-Wtype-limits"
@@ -449,7 +452,8 @@ poll (struct pollfd *pfd, nfds_t nfd, int timeout)
   static HANDLE hEvent;
   WSANETWORKEVENTS ev;
   HANDLE h, handle_array[FD_SETSIZE + 2];
-  DWORD ret, wait_timeout, nhandles, start = 0, elapsed, orig_timeout = 0;
+  DWORD ret, wait_timeout, nhandles, orig_timeout = 0;
+  ULONGLONG start = 0;
   fd_set rfds, wfds, xfds;
   BOOL poll_again;
   MSG msg;
@@ -465,7 +469,7 @@ poll (struct pollfd *pfd, nfds_t nfd, int timeout)
   if (timeout != INFTIM)
     {
       orig_timeout = timeout;
-      start = GetTickCount();
+      start = GetTickCount64();
     }
 
   if (!hEvent)
@@ -614,8 +618,8 @@ poll (struct pollfd *pfd, nfds_t nfd, int timeout)
 
   if (!rc && orig_timeout && timeout != INFTIM)
     {
-      elapsed = GetTickCount() - start;
-      timeout = elapsed >= orig_timeout ? 0 : orig_timeout - elapsed;
+      ULONGLONG elapsed = GetTickCount64() - start;
+      timeout = elapsed >= orig_timeout ? 0 : (int)(orig_timeout - elapsed);
     }
 
   if (!rc && timeout)
-- 
2.19.1-816-gcd69ec8cde
Previous: Johannes SixtNext: Randall S. Becker
Message 11 of 17 in “Make compat/poll safer on Windows”
  1. 0/1 Make compat/poll safer on WindowsJohannes Schindelin via GitGitGadget, Oct 31, 2018
  2. 1/1 poll: use GetTickCount64() to avoid wrap-around issuesSteve Hoelzer via GitGitGadget, Oct 31, 2018
  3. Johannes SixtNov 1, 2018
  4. Steve HoelzerNov 2, 2018
  5. Johannes SixtNov 2, 2018
  6. Steve HoelzerNov 2, 2018
  7. Junio C HamanoNov 3, 2018
  8. Junio C HamanoNov 3, 2018
  9. Carlo ArenasNov 3, 2018
  10. Johannes SixtNov 3, 2018
  11. Junio C HamanoNov 4, 2018
  12. Randall S. BeckerNov 4, 2018
  13. Eric SunshineNov 5, 2018
  14. Junio C HamanoNov 5, 2018
  15. Johannes SixtNov 5, 2018
  16. Johannes SixtNov 5, 2018
  17. Johannes SchindelinNov 5, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.