[ovs-dev] [PATCH] include/windows/unistd.h: fixed type cast warning on Windows

Michael Santana msantana at redhat.com
Thu Sep 2 17:04:46 UTC 2021



On 8/26/21 11:45 AM, Sergey Madaminov wrote:
> Currently, the function call type cast for getting file handle
> produces a warning during OvS compilation on Windows with the following
> message:
> 
> ```
> ..\include\windows\unistd.h:97:25: warning: cast from function call of type
> 'intptr_t' (aka 'int') to non-matching type 'HANDLE' (aka 'void *')
> [-Wbad-function-cast]
>      HANDLE h = (HANDLE) _get_osfhandle(fd);
> ```
> 
> There is a function `LongToHandle()` to perform such cast [1].
> But as `intptr_t` can be either `long long` for 64-bit or `int` for
> 32-bit, instead of clogging the code with `#ifdef` macros to use
> different cast functions, we can perform this cast directly.
> 
> Signed-off-by: Sergey Madaminov <sergey.madaminov at gmail.com>
> 
> ---
>   include/windows/unistd.h | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/include/windows/unistd.h b/include/windows/unistd.h
> index 21cc56ff1..62ff90a3f 100644
> --- a/include/windows/unistd.h
> +++ b/include/windows/unistd.h
> @@ -94,7 +94,7 @@ __inline long sysconf(int type)
>   static __inline int
>   rpl_isatty(int fd)
>   {
> -    HANDLE h = (HANDLE) _get_osfhandle(fd);
> +    HANDLE h = (HANDLE)(INT_PTR) _get_osfhandle(fd);
I dont have a windows machine handy to test INT_PTR so I did what I 
could to test it on a little snippet test code on my 64 bit linux machine.

void* bar = (void*) (int64_t*) myfunc32(foo);
This still warns me about -Wbad-function-cast

void* bar = (void*) (int64_t*) (int64_t) myfunc32(foo);
This does not warn me about -Wbad-function-cast. Is this how INT_PTR 
behaves on windows? It first casts to the correct size and then cast to 
pointer of that size as well like shown above?
>       DWORD st;
>       return (_isatty(STDOUT_FILENO)
>               && h != INVALID_HANDLE_VALUE
> 



More information about the dev mailing list