Resolve integer type conversion problem on MSVC

MSVC rightfully complained that there was some conversion from `size_t`
to `unsigned int` that could come with a loss of data. This commit
re-types the corresponding struct field `ctx_buffer::len` to `size_t`.
Also, the function `ctx_buffer_append` has an integer return value
which is supposed to be the (positive) length of the appended data
on success, and a check is inserted that the data to be appended does
not exceed MAX_INT in length.
This commit is contained in:
Hanno Becker 2017-12-06 08:35:02 +00:00
parent 6e5dd79a43
commit a5e68979ca

View File

@ -311,7 +311,7 @@ typedef struct
unsigned num_datagrams; unsigned num_datagrams;
unsigned char data[MAX_MSG_SIZE]; unsigned char data[MAX_MSG_SIZE];
unsigned len; size_t len;
} ctx_buffer; } ctx_buffer;
@ -323,7 +323,7 @@ static int ctx_buffer_flush( ctx_buffer *buf )
mbedtls_printf( " %05u flush %s: %u bytes, %u datagrams, last %u ms\n", mbedtls_printf( " %05u flush %s: %u bytes, %u datagrams, last %u ms\n",
ellapsed_time(), buf->description, ellapsed_time(), buf->description,
buf->len, buf->num_datagrams, (unsigned) buf->len, buf->num_datagrams,
ellapsed_time() - buf->packet_lifetime ); ellapsed_time() - buf->packet_lifetime );
ret = mbedtls_net_send( buf->ctx, buf->data, buf->len ); ret = mbedtls_net_send( buf->ctx, buf->data, buf->len );
@ -353,6 +353,9 @@ static int ctx_buffer_append( ctx_buffer *buf,
{ {
int ret; int ret;
if( len > (size_t) INT_MAX )
return( -1 );
if( len > sizeof( buf->data ) ) if( len > sizeof( buf->data ) )
{ {
mbedtls_printf( " ! buffer size %u too large (max %u)\n", mbedtls_printf( " ! buffer size %u too large (max %u)\n",
@ -372,7 +375,7 @@ static int ctx_buffer_append( ctx_buffer *buf,
if( ++buf->num_datagrams == 1 ) if( ++buf->num_datagrams == 1 )
buf->packet_lifetime = ellapsed_time(); buf->packet_lifetime = ellapsed_time();
return( len ); return( (int) len );
} }
#endif /* MBEDTLS_TIMING_C */ #endif /* MBEDTLS_TIMING_C */