From 640bb4e66751f82b43d42771be331181a9b509c8 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Tue, 2 Mar 2021 16:35:52 +0000 Subject: [PATCH] variables: Remove size changing warnings when using Visual Studio Change some of the types of variables, and cast change sizes where known to be safe. There still are some C4116 warning and a couple of C4090 warning which are OK, so disabled by a #pragma. --- examples/coap-client.c | 1 + examples/coap-server.c | 5 +++- include/coap2/coap_block_internal.h | 4 ++-- include/coap2/coap_prng.h | 2 +- include/coap2/coap_session.h | 4 ++-- include/coap2/encode.h | 4 ++-- include/coap2/pdu.h | 2 +- src/block.c | 32 ++++++++++++++------------ src/coap_gnutls.c | 3 ++- src/coap_io.c | 1 + src/coap_mbedtls.c | 8 +++---- src/coap_openssl.c | 8 +++---- src/coap_prng.c | 2 +- src/encode.c | 4 ++-- src/net.c | 4 ++-- src/pdu.c | 33 +++++++++++++++------------ win32/coap-client/coap-client.vcxproj | 2 +- 17 files changed, 65 insertions(+), 54 deletions(-) diff --git a/examples/coap-client.c b/examples/coap-client.c index fdd749f6..5d2dd424 100644 --- a/examples/coap-client.c +++ b/examples/coap-client.c @@ -18,6 +18,7 @@ #ifdef _WIN32 #define strcasecmp _stricmp #define strncasecmp _strnicmp +#define fileno _fileno #include "getopt.c" #if !defined(S_ISDIR) #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) diff --git a/examples/coap-server.c b/examples/coap-server.c index b10764c2..dda5ae0f 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -631,7 +631,7 @@ resolve_address(const coap_str_const_t *server, struct sockaddr *dst) { switch (ainfo->ai_family) { case AF_INET6: case AF_INET: - len = ainfo->ai_addrlen; + len = (int)ainfo->ai_addrlen; memcpy(dst, ainfo->ai_addr, len); goto finish; default: @@ -2898,6 +2898,9 @@ main(int argc, char **argv) { free(proxy_list); proxy_list = NULL; proxy_list_count = 0; +#ifdef _WIN32 +#pragma warning( disable : 4090 ) +#endif coap_free(proxy_host_name_list); #endif /* SERVER_CAN_PROXY */ diff --git a/include/coap2/coap_block_internal.h b/include/coap2/coap_block_internal.h index 50e4773d..1ce36dbc 100644 --- a/include/coap2/coap_block_internal.h +++ b/include/coap2/coap_block_internal.h @@ -105,7 +105,7 @@ struct coap_lg_crcv_t { uint8_t last_type; /**< Last request type (CON/NON) */ uint8_t initial; /**< If set, has not been used yet */ uint8_t szx; /**< size of individual blocks */ - uint32_t total_len; /**< Length as indicated by SIZE2 option */ + size_t total_len; /**< Length as indicated by SIZE2 option */ coap_binary_t *body_data; /**< Used for re-assembling entire body */ coap_binary_t *app_token; /**< app requesting PDU token */ uint8_t base_token[8]; /**< established base PDU token */ @@ -132,7 +132,7 @@ struct coap_lg_srcv_t { uint16_t content_format; /**< Content format for the set of blocks */ uint8_t last_type; /**< Last request type (CON/NON) */ uint8_t szx; /**< size of individual blocks */ - uint32_t total_len; /**< Length as indicated by SIZE1 option */ + size_t total_len; /**< Length as indicated by SIZE1 option */ coap_binary_t *body_data; /**< Used for re-assembling entire body */ size_t amount_so_far; /**< Amount of data seen so far */ coap_resource_t *resource; /**< associated resource */ diff --git a/include/coap2/coap_prng.h b/include/coap2/coap_prng.h index b8564031..ac26ae2f 100644 --- a/include/coap2/coap_prng.h +++ b/include/coap2/coap_prng.h @@ -89,7 +89,7 @@ void coap_set_prng(coap_rand_func_t rng); * * @param seed The seed for the pseudo random number generator. */ -void coap_prng_init(unsigned long seed); +void coap_prng_init(unsigned int seed); /** * Fills @p buf with @p len random bytes using the default pseudo diff --git a/include/coap2/coap_session.h b/include/coap2/coap_session.h index b692785e..6fc4dffe 100644 --- a/include/coap2/coap_session.h +++ b/include/coap2/coap_session.h @@ -62,8 +62,8 @@ typedef struct coap_session_t { coap_session_type_t type; /**< client or server side socket */ coap_session_state_t state; /**< current state of relationaship with peer */ unsigned ref; /**< reference count from queues */ - unsigned tls_overhead; /**< overhead of TLS layer */ - uint64_t mtu; /**< path or CSM mtu */ + size_t tls_overhead; /**< overhead of TLS layer */ + size_t mtu; /**< path or CSM mtu */ coap_address_t local_if; /**< optional local interface address */ UT_hash_handle hh; coap_addr_tuple_t addr_info; /**< key: remote/local address info */ diff --git a/include/coap2/encode.h b/include/coap2/encode.h index d3d72c20..11efb782 100644 --- a/include/coap2/encode.h +++ b/include/coap2/encode.h @@ -47,7 +47,7 @@ extern int coap_flsll(long long i); * * @return The decoded value */ -unsigned int coap_decode_var_bytes(const uint8_t *buf, unsigned int length); +unsigned int coap_decode_var_bytes(const uint8_t *buf, size_t length); /** * Decodes multiple-length byte sequences. @p buf points to an input byte @@ -58,7 +58,7 @@ unsigned int coap_decode_var_bytes(const uint8_t *buf, unsigned int length); * * @return The decoded value */ -uint64_t coap_decode_var_bytes8(const uint8_t *buf, unsigned int length); +uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t length); /** * Encodes multiple-length byte sequences. @p buf points to an output buffer of diff --git a/include/coap2/pdu.h b/include/coap2/pdu.h index 498c178b..9130e301 100644 --- a/include/coap2/pdu.h +++ b/include/coap2/pdu.h @@ -618,7 +618,7 @@ size_t coap_insert_option(coap_pdu_t *pdu, uint16_t type, * * Internal use only. */ -int coap_update_option(coap_pdu_t *pdu, +size_t coap_update_option(coap_pdu_t *pdu, uint16_t type, size_t len, const uint8_t *data); diff --git a/src/block.c b/src/block.c index 1d922280..51eee989 100644 --- a/src/block.c +++ b/src/block.c @@ -130,7 +130,7 @@ coap_add_block(coap_pdu_t *pdu, size_t len, const uint8_t *data, return 0; return coap_add_data(pdu, - min(len - start, (1ULL << (block_szx + 4))), + min(len - start, ((size_t)1 << (block_szx + 4))), data + start); } @@ -276,7 +276,7 @@ coap_context_set_block_mode(coap_context_t *context, COAP_STATIC_INLINE int block_token_match(const uint8_t *a, size_t alen, const uint8_t *b, size_t blen) { - uint32_t bias; + size_t bias; if (blen < 4) return alen == blen && memcmp(a, b, blen) == 0; bias = blen - 4; @@ -494,7 +494,8 @@ coap_add_data_large_internal(coap_session_t *session, coap_update_token(pdu, lg_xmit->b.b1.token_length, lg_xmit->b.b1.token); coap_update_option(pdu, COAP_OPTION_SIZE1, - coap_encode_var_safe(buf, sizeof(buf), length), + coap_encode_var_safe(buf, sizeof(buf), + (unsigned int)length), buf); } else { @@ -524,7 +525,8 @@ coap_add_data_large_internal(coap_session_t *session, } coap_update_option(pdu, COAP_OPTION_SIZE2, - coap_encode_var_safe(buf, sizeof(buf), length), + coap_encode_var_safe(buf, sizeof(buf), + (unsigned int)length), buf); if (etag == 0) { if (++session->context->etag == 0) @@ -778,7 +780,7 @@ check_if_received_block(coap_rblock_t *rec_blocks, uint32_t block_num) { } static int -check_all_blocks_in(coap_rblock_t *rec_blocks, uint32_t total_blocks) { +check_all_blocks_in(coap_rblock_t *rec_blocks, size_t total_blocks) { uint32_t i; uint32_t block = 0; @@ -1007,15 +1009,15 @@ coap_handle_request_send_block(coap_session_t *session, chunk = (size_t)1 << (p->blk_size + 4); if (block_opt) { coap_log(LOG_DEBUG, - "found Block option, block size is %u, block nr. %u, M %d\n", - 1 << (block.szx + 4), block.num, block.m); + "found Block option, block size is %zu, block nr. %u, M %d\n", + (size_t)1 << (block.szx + 4), block.num, block.m); if (block.szx != p->blk_size) { - if ((p->offset + chunk) % (1 << (block.szx + 4)) == 0) { + if ((p->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) { /* * Recompute the block number of the previous packet given * the new block size */ - block.num = ((p->offset + chunk) >> (block.szx + 4)) - 1; + block.num = (uint32_t)(((p->offset + chunk) >> (block.szx + 4)) - 1); p->blk_size = block.szx; chunk = (size_t)1 << (p->blk_size + 4); p->offset = block.num * chunk; @@ -1029,7 +1031,7 @@ coap_handle_request_send_block(coap_session_t *session, "boundary. (%zu x %u mod %u = %zu (which is not 0)\n", p->offset/chunk + 1, (1 << (p->blk_size + 4)), (1 << (block.szx + 4)), - (p->offset + chunk) % (1 << (block.szx + 4))); + (p->offset + chunk) % ((size_t)1 << (block.szx + 4))); } } } @@ -1345,7 +1347,7 @@ coap_handle_request_put_block(coap_context_t *context, } if (!check_all_blocks_in(&p->rec_blocks, - (p->total_len + chunk -1)/chunk)) { + (uint32_t)(p->total_len + chunk -1)/chunk)) { /* Not all the payloads of the body have arrived */ if (block.m) { uint8_t buf[4]; @@ -1470,12 +1472,12 @@ coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *rcvd) { "found Block option, block size is %u, block nr. %u\n", 1 << (block.szx + 4), block.num); if (block.szx != p->blk_size) { - if ((p->offset + chunk) % (1 << (block.szx + 4)) == 0) { + if ((p->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) { /* * Recompute the block number of the previous packet given the * new block size */ - block.num = ((p->offset + chunk) >> (block.szx + 4)) - 1; + block.num = (uint32_t)(((p->offset + chunk) >> (block.szx + 4)) - 1); p->blk_size = block.szx; chunk = (size_t)1 << (p->blk_size + 4); p->offset = block.num * chunk; @@ -1488,7 +1490,7 @@ coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *rcvd) { "(%zu x %u mod %u = %zu != 0)\n", p->offset/chunk + 1, (1 << (p->blk_size + 4)), (1 << (block.szx + 4)), - (p->offset + chunk) % (1 << (block.szx + 4))); + (p->offset + chunk) % ((size_t)1 << (block.szx + 4))); } } if (p->last_block == (int)block.num) { @@ -1645,7 +1647,7 @@ coap_handle_response_get_block(coap_context_t *context, uint8_t *data; coap_opt_t *size_opt = coap_check_option(rcvd, COAP_OPTION_SIZE2, &opt_iter); - uint32_t size2 = size_opt ? + size_t size2 = size_opt ? coap_decode_var_bytes(coap_opt_value(size_opt), coap_opt_length(size_opt)) : 0; diff --git a/src/coap_gnutls.c b/src/coap_gnutls.c index e15ad081..139169d4 100644 --- a/src/coap_gnutls.c +++ b/src/coap_gnutls.c @@ -2215,7 +2215,8 @@ void coap_dtls_session_update_mtu(coap_session_t *c_session) { int ret; if (g_env) - G_CHECK(gnutls_dtls_set_data_mtu(g_env->g_session, c_session->mtu), + G_CHECK(gnutls_dtls_set_data_mtu(g_env->g_session, + (unsigned int)c_session->mtu), "gnutls_dtls_set_data_mtu"); fail: ;; diff --git a/src/coap_io.c b/src/coap_io.c index 9bebffce..6f9205d1 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -542,6 +542,7 @@ static __declspec(thread) LPFN_WSARECVMSG lpWSARecvMsg = NULL; #undef CMSG_DATA #define CMSG_DATA WSA_CMSG_DATA #define ipi_spec_dst ipi_addr +#pragma warning( disable : 4116 ) #else #define iov_len_t size_t #endif diff --git a/src/coap_mbedtls.c b/src/coap_mbedtls.c index 53c93ad2..d4edca12 100644 --- a/src/coap_mbedtls.c +++ b/src/coap_mbedtls.c @@ -938,7 +938,7 @@ static int setup_server_ssl_session(coap_session_t *c_session, mbedtls_ssl_cookie_check, &m_env->cookie_ctx ); #if MBEDTLS_VERSION_NUMBER >= 0x02100100 - mbedtls_ssl_set_mtu(&m_env->ssl, c_session->mtu); + mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->mtu); #endif /* MBEDTLS_VERSION_NUMBER >= 0x02100100 */ #endif /* MBEDTLS_SSL_PROTO_DTLS */ fail: @@ -1111,7 +1111,7 @@ static int setup_client_ssl_session(coap_session_t *c_session, } #if defined(MBEDTLS_SSL_PROTO_DTLS) #if MBEDTLS_VERSION_NUMBER >= 0x02100100 - mbedtls_ssl_set_mtu(&m_env->ssl, c_session->mtu); + mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->mtu); #endif /* MBEDTLS_VERSION_NUMBER >= 0x02100100 */ #endif /* MBEDTLS_SSL_PROTO_DTLS */ set_ciphersuites(&m_env->conf, COAP_ENC_PKI); @@ -1543,7 +1543,7 @@ void *coap_dtls_new_server_session(coap_session_t *c_session) if (m_env) { #if defined(MBEDTLS_SSL_PROTO_DTLS) #if MBEDTLS_VERSION_NUMBER >= 0x02100100 - mbedtls_ssl_set_mtu(&m_env->ssl, c_session->mtu); + mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->mtu); #endif /* MBEDTLS_VERSION_NUMBER >= 0x02100100 */ #endif /* MBEDTLS_SSL_PROTO_DTLS */ } @@ -1567,7 +1567,7 @@ void coap_dtls_session_update_mtu(coap_session_t *c_session) (coap_mbedtls_env_t *)c_session->tls; if (m_env) { #if MBEDTLS_VERSION_NUMBER >= 0x02100100 - mbedtls_ssl_set_mtu(&m_env->ssl, c_session->mtu); + mbedtls_ssl_set_mtu(&m_env->ssl, (uint16_t)c_session->mtu); #endif /* MBEDTLS_VERSION_NUMBER >= 0x02100100 */ } #endif /* MBEDTLS_SSL_PROTO_DTLS */ diff --git a/src/coap_openssl.c b/src/coap_openssl.c index a8f55cb8..371cbd3f 100644 --- a/src/coap_openssl.c +++ b/src/coap_openssl.c @@ -2754,7 +2754,7 @@ void * coap_dtls_new_server_session(coap_session_t *session) { SSL_set_bio(nssl, nbio, nbio); SSL_set_app_data(nssl, NULL); SSL_set_options(nssl, SSL_OP_COOKIE_EXCHANGE); - SSL_set_mtu(nssl, session->mtu); + SSL_set_mtu(nssl, (long)session->mtu); ssl = dtls->ssl; dtls->ssl = nssl; nssl = NULL; @@ -2888,7 +2888,7 @@ void *coap_dtls_new_client_session(coap_session_t *session) { SSL_set_bio(ssl, bio, bio); SSL_set_app_data(ssl, session); SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE); - SSL_set_mtu(ssl, session->mtu); + SSL_set_mtu(ssl, (long)session->mtu); if (!setup_client_ssl_session(session, ssl)) goto error; @@ -2916,7 +2916,7 @@ error: void coap_dtls_session_update_mtu(coap_session_t *session) { SSL *ssl = (SSL *)session->tls; if (ssl) - SSL_set_mtu(ssl, session->mtu); + SSL_set_mtu(ssl, (long)session->mtu); } void coap_dtls_free_session(coap_session_t *session) { @@ -3006,7 +3006,7 @@ int coap_dtls_hello(coap_session_t *session, coap_ssl_data *ssl_data; int r; - SSL_set_mtu(dtls->ssl, session->mtu); + SSL_set_mtu(dtls->ssl, (long)session->mtu); ssl_data = (coap_ssl_data*)BIO_get_data(SSL_get_rbio(dtls->ssl)); assert(ssl_data != NULL); if (ssl_data->pdu_len) { diff --git a/src/coap_prng.c b/src/coap_prng.c index 888a9f03..7bbb9bbf 100644 --- a/src/coap_prng.c +++ b/src/coap_prng.c @@ -71,7 +71,7 @@ coap_set_prng(coap_rand_func_t rng) { } void -coap_prng_init(unsigned long seed) { +coap_prng_init(unsigned int seed) { #ifdef HAVE_GETRANDOM /* No seed to seed the random source if getrandom() is used, * see dtls_prng(). */ diff --git a/src/encode.c b/src/encode.c index 2ff70d68..759978da 100644 --- a/src/encode.c +++ b/src/encode.c @@ -26,7 +26,7 @@ int coap_flsll(long long i) #endif unsigned int -coap_decode_var_bytes(const uint8_t *buf,unsigned int len) { +coap_decode_var_bytes(const uint8_t *buf, size_t len) { unsigned int i, n = 0; for (i = 0; i < len; ++i) n = (n << 8) + buf[i]; @@ -55,7 +55,7 @@ coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val) { } uint64_t -coap_decode_var_bytes8(const uint8_t *buf,unsigned int len) { +coap_decode_var_bytes8(const uint8_t *buf, size_t len) { unsigned int i; uint64_t n = 0; for (i = 0; i < len; ++i) diff --git a/src/net.c b/src/net.c index 536fd375..6e1fc574 100644 --- a/src/net.c +++ b/src/net.c @@ -1095,7 +1095,7 @@ coap_send(coap_session_t *session, coap_pdu_t *pdu) { if (coap_print_addr(&session->addr_info.local, (uint8_t*)addr_str, sizeof(addr_str) - 1)) { char *cp; - int len; + size_t len; if (addr_str[0] == '[') { cp = strchr(addr_str, ']'); @@ -3094,7 +3094,7 @@ void coap_startup(void) { coap_ticks(&now); us = coap_ticks_to_rt_us(now); /* Be accurate to the nearest (approx) us */ - coap_prng_init(us); + coap_prng_init((unsigned int)us); coap_memory_init(); coap_dtls_startup(); } diff --git a/src/pdu.c b/src/pdu.c index db8051ba..1bfe5805 100644 --- a/src/pdu.c +++ b/src/pdu.c @@ -334,17 +334,17 @@ coap_remove_option(coap_pdu_t *pdu, uint16_t type) { opt_delta = decode_this.delta + decode_next.delta; if (opt_delta <= 12) { /* can simply update the delta of next option */ - next_option[0] = (next_option[0] & 0x0f) + (opt_delta << 4); + next_option[0] = (next_option[0] & 0x0f) + (coap_opt_t)(opt_delta << 4); } else if (opt_delta <= 269 && decode_next.delta <= 12) { /* next option delta size increase */ next_option -= 1; next_option[0] = (next_option[1] & 0x0f) + (13 << 4); - next_option[1] = opt_delta - 13; + next_option[1] = (coap_opt_t)(opt_delta - 13); } else if (opt_delta <= 269) { /* can simply update the delta of next option */ - next_option[1] = opt_delta - 13; + next_option[1] = (coap_opt_t)(opt_delta - 13); } else if (decode_next.delta <= 12) { /* next option delta size increase */ @@ -373,18 +373,18 @@ coap_remove_option(coap_pdu_t *pdu, uint16_t type) { } next_option -= 2; next_option[0] = (next_option[2] & 0x0f) + (14 << 4); - next_option[1] = (opt_delta - 269) >> 8; + next_option[1] = (coap_opt_t)((opt_delta - 269) >> 8); next_option[2] = (opt_delta - 269) & 0xff; } else if (decode_next.delta <= 269) { /* next option delta size increase */ next_option -= 1; next_option[0] = (next_option[1] & 0x0f) + (14 << 4); - next_option[1] = (opt_delta - 269) >> 8; + next_option[1] = (coap_opt_t)((opt_delta - 269) >> 8); next_option[2] = (opt_delta - 269) & 0xff; } else { - next_option[1] = (opt_delta - 269) >> 8; + next_option[1] = (coap_opt_t)((opt_delta - 269) >> 8); next_option[2] = (opt_delta - 269) & 0xff; } } @@ -450,31 +450,31 @@ coap_insert_option(coap_pdu_t *pdu, uint16_t type, size_t len, if (decode.delta <= 12) { /* can simply patch in the new delta of next option */ - option[0] = (option[0] & 0x0f) + (opt_delta << 4); + option[0] = (option[0] & 0x0f) + (coap_opt_t)(opt_delta << 4); } else if (decode.delta <= 269 && opt_delta <= 12) { /* option header is going to shrink by one byte */ - option[1] = (option[0] & 0x0f) + (opt_delta << 4); + option[1] = (option[0] & 0x0f) + (coap_opt_t)(opt_delta << 4); shrink = 1; } else if (decode.delta <= 269 && opt_delta <= 269) { /* can simply patch in the new delta of next option */ - option[1] = opt_delta - 13; + option[1] = (coap_opt_t)(opt_delta - 13); } else if (opt_delta <= 12) { /* option header is going to shrink by two bytes */ - option[2] = (option[0] & 0x0f) + (opt_delta << 4); + option[2] = (option[0] & 0x0f) + (coap_opt_t)(opt_delta << 4); shrink = 2; } else if (opt_delta <= 269) { /* option header is going to shrink by one bytes */ option[1] = (option[0] & 0x0f) + 0xd0; - option[2] = opt_delta - 13; + option[2] = (coap_opt_t)(opt_delta - 13); shrink = 1; } else { /* can simply patch in the new delta of next option */ - option[1] = (opt_delta - 269) >> 8; + option[1] = (coap_opt_t)((opt_delta - 269) >> 8); option[2] = (opt_delta - 269) & 0xff; } @@ -490,7 +490,7 @@ coap_insert_option(coap_pdu_t *pdu, uint16_t type, size_t len, return shift; } -int +size_t coap_update_option(coap_pdu_t *pdu, uint16_t type, size_t len, const uint8_t *data) { coap_opt_iterator_t opt_iter; @@ -646,6 +646,9 @@ coap_get_data(const coap_pdu_t *pdu, size_t *len, uint8_t **data) { size_t total; const uint8_t **cdata = NULL; +#ifdef _WIN32 +#pragma warning( disable : 4090 ) +#endif /* Really, coap_get_data() should be using a const */ memcpy(&cdata, &data, sizeof(cdata)); return coap_get_data_large(pdu, len, cdata, &offset, &total); @@ -1023,9 +1026,9 @@ coap_pdu_parse_opt(coap_pdu_t *pdu) { */ static char outbuf[COAP_DEBUG_BUF_SIZE]; char *obp; - uint32_t tlen; + size_t tlen; size_t outbuflen; - size_t i; + int i; int ok; for (i = 0; i < 2; i++) { diff --git a/win32/coap-client/coap-client.vcxproj b/win32/coap-client/coap-client.vcxproj index bd86abe8..4e475fcb 100644 --- a/win32/coap-client/coap-client.vcxproj +++ b/win32/coap-client/coap-client.vcxproj @@ -333,7 +333,7 @@ - +