New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: silence benign warning with pragma #3263

Closed
wants to merge 2 commits into
base: master
from

Conversation

Projects
None yet
3 participants
@danielgustafsson
Member

danielgustafsson commented Nov 12, 2018

Disclaimer: I don't have a system to reproduce the warning, and test the fix, on so this is perhaps more of a discussion piece than a patch. Anyways, here goes.

Commit 5bfaa86 introduced a new compiler warning on Windows cross compilation with GCC. The coding is however a documented pattern for acquiring a handle to write to, so rather than changing working code this wraps the offending cast in a GCC diagnostic pragma to silence the warning. See below for an example of the warning from the autobuild logs:

/src/tool_cb_wrt.c:175:9: warning: cast from function call of type 'intptr_t {aka long long int}' to non-matching type 'void *' [-Wbad-function-cast]
(HANDLE) _get_osfhandle(fileno(outs->stream)),
^

An example failing build can be found here: https://curl.haxx.se/dev/log.cgi?id=20181111190642-17820#prob1

tool_cb_wrt.c: silence benign warning with pragma
Commit 5bfaa86 introduced a new
compiler warning on Windows cross compilation with GCC. The coding
is however a documented pattern for acquiring a handle to write to,
so rather than changing working code this wraps the offending cast
in a GCC diagnostic pragma to silence the warning. See below for an
example of the warning from the autobuild logs (whitespace edited
to fit):

/src/tool_cb_wrt.c:175:9: warning: cast from function call of type
    'intptr_t {aka long long int}' to non-matching type 'void *'
    [-Wbad-function-cast]
(HANDLE) _get_osfhandle(fileno(outs->stream)),
^
@MarcelRaad

This comment has been minimized.

Member

MarcelRaad commented Nov 12, 2018

I think you just have to store the result of the function call in an intermediate variable to suppress the warning. That would maybe be less verbose? At least that's what I did in other places.

@vszakats

This comment has been minimized.

Member

vszakats commented Nov 12, 2018

According to this, this workaround will silence it without using pragmas:

--- a/src/tool_cb_wrt.c
+++ b/src/tool_cb_wrt.c
@@ -161,6 +161,7 @@ size_t tool_write_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
     DWORD in_len = (DWORD)(sz * nmemb);
     wchar_t* wc_buf;
     DWORD wc_len;
+    intptr_t fhnd = _get_osfhandle(fileno(outs->stream));
 
     /* calculate buffer size for wide characters */
     wc_len = MultiByteToWideChar(CP_UTF8, 0, buffer, in_len,  NULL, 0);
@@ -172,7 +173,7 @@ size_t tool_write_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
     wc_len = MultiByteToWideChar(CP_UTF8, 0, buffer, in_len, wc_buf, wc_len);
 
     if(!WriteConsoleW(
-        (HANDLE) _get_osfhandle(fileno(outs->stream)),
+        (HANDLE) fhnd,
         wc_buf,
         wc_len,
         &wc_len,

I can confirm the above silences the warning, using this command:

$ x86_64-w64-mingw32-gcc -c tool_cb_wrt.c -I../include -I../lib -Wbad-function-cast

(One issue with #pragma GCC diagnostic is that it requires at least gcc 4.6.)

tool_cb_wrt: Silence compiler warning
Commit 5bfaa86 introduced a new
compiler warning on Windows cross compilation with GCC. See below
for an example of the warning from the autobuild logs (whitespace
edited to fit):

/src/tool_cb_wrt.c:175:9: warning: cast from function call of type
    'intptr_t {aka long long int}' to non-matching type 'void *'
    [-Wbad-function-cast]
(HANDLE) _get_osfhandle(fileno(outs->stream)),
^

Store the return value from _get_osfhandle() in an intermediate
variable and cast the variable in WriteConsoleW() rather than the
function call directly to avoid a compiler warning.

In passing, also add inspection of the MultiByteToWideChar() return
value and return failure in case an error is reported.
@danielgustafsson

This comment has been minimized.

Member

danielgustafsson commented Nov 13, 2018

Perfect, that's a much better solution! I've pushed an updated commit which will be squashed if accepted. In passing I also noticed that the possibility of an error from MultiByteToWideChar() wasn't considered so added a check for that as well while in the codepath.

@MarcelRaad

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment