Fix MSVC warnings in library and programs

parent 7e2d68c1
...@@ -143,7 +143,7 @@ static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length, ...@@ -143,7 +143,7 @@ static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length,
{ {
int ret; int ret;
unsigned char i; unsigned char i;
unsigned char q = 16 - 1 - iv_len; unsigned char q;
size_t len_left, olen; size_t len_left, olen;
unsigned char b[16]; unsigned char b[16];
unsigned char y[16]; unsigned char y[16];
...@@ -166,6 +166,8 @@ static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length, ...@@ -166,6 +166,8 @@ static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length,
if( add_len > 0xFF00 ) if( add_len > 0xFF00 )
return( MBEDTLS_ERR_CCM_BAD_INPUT ); return( MBEDTLS_ERR_CCM_BAD_INPUT );
q = 16 - 1 - (unsigned char) iv_len;
/* /*
* First block B_0: * First block B_0:
* 0 .. 0 flags * 0 .. 0 flags
...@@ -257,7 +259,7 @@ static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length, ...@@ -257,7 +259,7 @@ static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length,
while( len_left > 0 ) while( len_left > 0 )
{ {
unsigned char use_len = len_left > 16 ? 16 : len_left; size_t use_len = len_left > 16 ? 16 : len_left;
if( mode == CCM_ENCRYPT ) if( mode == CCM_ENCRYPT )
{ {
......
...@@ -74,6 +74,14 @@ static int wsa_init_done = 0; ...@@ -74,6 +74,14 @@ static int wsa_init_done = 0;
#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */ #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 <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
...@@ -150,7 +158,7 @@ int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, const char ...@@ -150,7 +158,7 @@ int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, const char
continue; continue;
} }
if( connect( ctx->fd, cur->ai_addr, cur->ai_addrlen ) == 0 ) if( connect( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) == 0 )
{ {
ret = 0; ret = 0;
break; break;
...@@ -208,7 +216,7 @@ int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char ...@@ -208,7 +216,7 @@ int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char
continue; continue;
} }
if( bind( ctx->fd, cur->ai_addr, cur->ai_addrlen ) != 0 ) if( bind( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) != 0 )
{ {
close( ctx->fd ); close( ctx->fd );
ret = MBEDTLS_ERR_NET_BIND_FAILED; ret = MBEDTLS_ERR_NET_BIND_FAILED;
......
...@@ -1022,11 +1022,11 @@ have_ciphersuite_v2: ...@@ -1022,11 +1022,11 @@ have_ciphersuite_v2:
static int ssl_parse_client_hello( mbedtls_ssl_context *ssl ) static int ssl_parse_client_hello( mbedtls_ssl_context *ssl )
{ {
int ret, got_common_suite; int ret, got_common_suite;
unsigned int i, j; size_t i, j;
unsigned int ciph_offset, comp_offset, ext_offset; size_t ciph_offset, comp_offset, ext_offset;
unsigned int msg_len, ciph_len, sess_len, comp_len, ext_len; size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
#if defined(MBEDTLS_SSL_PROTO_DTLS) #if defined(MBEDTLS_SSL_PROTO_DTLS)
unsigned int cookie_offset, cookie_len; size_t cookie_offset, cookie_len;
#endif #endif
unsigned char *buf, *p, *ext; unsigned char *buf, *p, *ext;
#if defined(MBEDTLS_SSL_RENEGOTIATION) #if defined(MBEDTLS_SSL_RENEGOTIATION)
...@@ -3025,8 +3025,18 @@ static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl, ...@@ -3025,8 +3025,18 @@ static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl,
} }
ssl->handshake->pmslen = 48; ssl->handshake->pmslen = 48;
/* mask = diff ? 0xff : 0x00 */ /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
/* MSVC has a warning about unary minus on unsigned, but this is
* well-defined and precisely what we want to do here */
#if defined(_MSC_VER)
#pragma warning( push )
#pragma warning( disable : 4146 )
#endif
mask = - ( ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ) ); mask = - ( ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ) );
#if defined(_MSC_VER)
#pragma warning( pop )
#endif
for( i = 0; i < ssl->handshake->pmslen; i++ ) for( i = 0; i < ssl->handshake->pmslen; i++ )
pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] ); pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
......
...@@ -1614,10 +1614,10 @@ static int ssl_decrypt_buf( mbedtls_ssl_context *ssl ) ...@@ -1614,10 +1614,10 @@ static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
unsigned char add_data[13]; unsigned char add_data[13];
unsigned char taglen = ssl->transform_in->ciphersuite_info->flags & unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
unsigned char explicit_iv_len = ssl->transform_in->ivlen - size_t explicit_iv_len = ssl->transform_in->ivlen -
ssl->transform_in->fixed_ivlen; ssl->transform_in->fixed_ivlen;
if( ssl->in_msglen < (size_t) explicit_iv_len + taglen ) if( ssl->in_msglen < explicit_iv_len + taglen )
{ {
MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) " MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
"+ taglen (%d)", ssl->in_msglen, "+ taglen (%d)", ssl->in_msglen,
...@@ -5793,7 +5793,7 @@ const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) ...@@ -5793,7 +5793,7 @@ const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl ) int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
{ {
int transform_expansion; size_t transform_expansion;
const mbedtls_ssl_transform *transform = ssl->transform_out; const mbedtls_ssl_transform *transform = ssl->transform_out;
#if defined(MBEDTLS_ZLIB_SUPPORT) #if defined(MBEDTLS_ZLIB_SUPPORT)
...@@ -5802,7 +5802,7 @@ int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl ) ...@@ -5802,7 +5802,7 @@ int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
#endif #endif
if( transform == NULL ) if( transform == NULL )
return( mbedtls_ssl_hdr_len( ssl ) ); return( (int) mbedtls_ssl_hdr_len( ssl ) );
switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) ) switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
{ {
...@@ -5822,7 +5822,7 @@ int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl ) ...@@ -5822,7 +5822,7 @@ int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
} }
return( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ); return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
} }
#if defined(MBEDTLS_X509_CRT_PARSE_C) #if defined(MBEDTLS_X509_CRT_PARSE_C)
......
...@@ -1330,7 +1330,7 @@ send_request: ...@@ -1330,7 +1330,7 @@ send_request:
len = mbedtls_snprintf( (char *) buf, sizeof(buf) - 1, GET_REQUEST, len = mbedtls_snprintf( (char *) buf, sizeof(buf) - 1, GET_REQUEST,
opt.request_page ); opt.request_page );
tail_len = strlen( GET_REQUEST_END ); tail_len = (int) strlen( GET_REQUEST_END );
/* Add padding to GET request to reach opt.request_size in length */ /* Add padding to GET request to reach opt.request_size in length */
if( opt.request_size != DFL_REQUEST_SIZE && if( opt.request_size != DFL_REQUEST_SIZE &&
......
...@@ -2049,7 +2049,7 @@ data_exchange: ...@@ -2049,7 +2049,7 @@ data_exchange:
unsigned char *larger_buf; unsigned char *larger_buf;
ori_len = ret; ori_len = ret;
extra_len = mbedtls_ssl_get_bytes_avail( &ssl ); extra_len = (int) mbedtls_ssl_get_bytes_avail( &ssl );
larger_buf = mbedtls_calloc( 1, ori_len + extra_len + 1 ); larger_buf = mbedtls_calloc( 1, ori_len + extra_len + 1 );
if( larger_buf == NULL ) if( larger_buf == NULL )
......
...@@ -491,7 +491,7 @@ int main( int argc, char *argv[] ) ...@@ -491,7 +491,7 @@ int main( int argc, char *argv[] )
*/ */
if( opt.seed == 0 ) if( opt.seed == 0 )
{ {
opt.seed = time( NULL ); opt.seed = (unsigned int) time( NULL );
mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed ); mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed );
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment