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

[PATCH 2/4] mingw: avoid warnings when casting HANDLEs to int

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Jan 15, 2016, 13:24 UTC
Message-ID
<61a69db5495c2ea16f98fab58e160dc575f73179.1452864241.git.johannes.schindelin@gmx.de>
In-Reply-To
<cover.1452864241.git.johannes.schindelin@gmx.de>

HANDLE is defined internally as a void *, but in many cases it is actually guaranteed to be a 32-bit integer. In these cases, GCC should not warn about a cast of a pointer to an integer of a different type because we know exactly what we are doing.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 compat/mingw.c     | 9 ++++++---
 compat/poll/poll.c | 2 +-
 compat/winansi.c   | 3 ++-
 3 files changed, 9 insertions(+), 5 deletions(-)
diff --git a/compat/mingw.c b/compat/mingw.c
index 1b3530a..8437c9a 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -6,6 +6,8 @@
 #include "../run-command.h"
 #include "../cache.h"
 
+#define HCAST(type, handle) ((type)(intptr_t)handle)
+
 static const int delay[] = { 0, 1, 10, 20, 40 };
 
 int err_win_to_posix(DWORD winerr)
@@ -691,13 +693,13 @@ int pipe(int filedes[2])
 		errno = err_win_to_posix(GetLastError());
 		return -1;
 	}
-	filedes[0] = _open_osfhandle((int)h[0], O_NOINHERIT);
+	filedes[0] = _open_osfhandle(HCAST(int, h[0]), O_NOINHERIT);
 	if (filedes[0] < 0) {
 		CloseHandle(h[0]);
 		CloseHandle(h[1]);
 		return -1;
 	}
-	filedes[1] = _open_osfhandle((int)h[1], O_NOINHERIT);
+	filedes[1] = _open_osfhandle(HCAST(int, h[1]), O_NOINHERIT);
 	if (filedes[1] < 0) {
 		close(filedes[0]);
 		CloseHandle(h[1]);
@@ -1846,7 +1848,8 @@ void mingw_open_html(const char *unixpath)
 		die("cannot run browser");
 
 	printf("Launching default browser to display HTML ...\n");
-	r = (int)ShellExecute(NULL, "open", htmlpath, NULL, "\\", SW_SHOWNORMAL);
+	r = HCAST(int, ShellExecute(NULL, "open", htmlpath,
+				NULL, "\\", SW_SHOWNORMAL));
 	FreeLibrary(shell32);
 	/* see the MSDN documentation referring to the result codes here */
 	if (r <= 32) {
diff --git a/compat/poll/poll.c b/compat/poll/poll.c
index db4e03e..b10adc7 100644
--- a/compat/poll/poll.c
+++ b/compat/poll/poll.c
@@ -76,7 +76,7 @@
 
 #ifdef WIN32_NATIVE
 
-#define IsConsoleHandle(h) (((long) (h) & 3) == 3)
+#define IsConsoleHandle(h) (((long) (intptr_t) (h) & 3) == 3)
 
 static BOOL
 IsSocketHandle (HANDLE h)
diff --git a/compat/winansi.c b/compat/winansi.c
index ceff55b..4549848 100644
--- a/compat/winansi.c
+++ b/compat/winansi.c
@@ -452,7 +452,8 @@ static HANDLE duplicate_handle(HANDLE hnd)
 	HANDLE hresult, hproc = GetCurrentProcess();
 	if (!DuplicateHandle(hproc, hnd, hproc, &hresult, 0, TRUE,
 			DUPLICATE_SAME_ACCESS))
-		die_lasterr("DuplicateHandle(%li) failed", (long) hnd);
+		die_lasterr("DuplicateHandle(%li) failed",
+			(long) (intptr_t) hnd);
 	return hresult;
 }
 
-- 
2.7.0.windows.1.7.g55a05c8
Previous: Johannes SchindelinNext: Johannes Schindelin
Message 3 of 7 in “Fix compile warnings with MSys2's compiler”
  1. 0/4 Fix compile warnings with MSys2's compilerJohannes Schindelin, Jan 15, 2016
  2. 1/4 mingw: avoid redefining S_* constantsJohannes Schindelin, Jan 15, 2016
  3. 2/4 mingw: avoid warnings when casting HANDLEs to intJohannes Schindelin, Jan 15, 2016
  4. 3/4 mingw: squash another warning about a castJohannes Schindelin, Jan 15, 2016
  5. 4/4 mingw: uglify (a, 0) definitions to shut up warningsJohannes Schindelin, Jan 15, 2016
  6. Junio C HamanoJan 15, 2016
  7. Johannes SchindelinJan 16, 2016

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.