From 6e5e9aaf7fb11208ff15335336b89c8737ff04df Mon Sep 17 00:00:00 2001 From: Andres Amaya Garcia Date: Thu, 6 Jul 2017 14:09:24 +0100 Subject: [PATCH] Fix MSVC warning in net.c The warning was caused because in MSVC some of the function parameters for the socket APIs are int while the fields in struct addrinfo are size_t e.g. possible data loss. --- library/net.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/library/net.c b/library/net.c index c7ce2584e3..f88b844ac2 100644 --- a/library/net.c +++ b/library/net.c @@ -92,6 +92,14 @@ static int wsa_init_done = 0; #endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */ +/* Some MS functions want int and MSVC warns if we pass size_t, + * but the standard fucntions use socklen_t, so cast only for MSVC */ +#if defined(_MSC_VER) +#define MSVC_INT_CAST (int) +#else +#define MSVC_INT_CAST +#endif + #include #include @@ -202,7 +210,7 @@ int net_connect( int *fd, const char *host, int port ) continue; } - if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 ) + if( connect( *fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) == 0 ) { ret = 0; break; @@ -299,7 +307,7 @@ int net_bind( int *fd, const char *bind_ip, int port ) continue; } - if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 ) + if( bind( *fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) != 0 ) { close( *fd ); ret = POLARSSL_ERR_NET_BIND_FAILED;