Skip to content

Commit

Permalink
Fix issue of conversion from size_t to int
Browse files Browse the repository at this point in the history
ssl_helpers.c is treated with W3 warning level in MSVC complier.
So that it's reported as error for warning of conversion from
size_t to int. This change fixes all this type of warning seen in
Microsoft Visual Studio 12.0. Besides, some potential problems of
type conversion are also handled.

Signed-off-by: Yanray Wang <[email protected]>
  • Loading branch information
Yanray Wang committed Mar 16, 2023
1 parent 89b4d12 commit d2696f2
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions tests/src/test_helpers/ssl_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf,
}

buf->content_length += input_len;
return input_len;
return (input_len > INT_MAX) ? INT_MAX : (int) input_len;
}

int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf,
Expand Down Expand Up @@ -171,7 +171,7 @@ int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf,
buf->content_length -= output_len;
buf->start = (buf->start + output_len) % buf->capacity;

return output_len;
return (output_len > INT_MAX) ? INT_MAX : (int) output_len;
}

int mbedtls_test_ssl_message_queue_setup(
Expand All @@ -182,7 +182,7 @@ int mbedtls_test_ssl_message_queue_setup(
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
}

queue->capacity = capacity;
queue->capacity = (capacity > INT_MAX) ? INT_MAX : (int) capacity;
queue->pos = 0;
queue->num = 0;

Expand Down Expand Up @@ -218,7 +218,7 @@ int mbedtls_test_ssl_message_queue_push_info(
place = (queue->pos + queue->num) % queue->capacity;
queue->messages[place] = len;
queue->num++;
return len;
return (len > INT_MAX) ? INT_MAX : (int) len;
}

int mbedtls_test_ssl_message_queue_pop_info(
Expand All @@ -241,7 +241,8 @@ int mbedtls_test_ssl_message_queue_pop_info(
queue->pos += queue->capacity;
}

return (message_length > buf_len) ? buf_len : message_length;
return (message_length > INT_MAX && buf_len > INT_MAX) ? INT_MAX :
(message_length > buf_len) ? (int) buf_len : (int) message_length;
}

/*
Expand Down Expand Up @@ -513,7 +514,7 @@ int mbedtls_test_mock_tcp_recv_msg(void *ctx,
}
mbedtls_test_ssl_message_queue_pop_info(queue, buf_len);

return msg_len;
return (msg_len > INT_MAX) ? INT_MAX : (int) msg_len;
}

#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
Expand Down Expand Up @@ -990,13 +991,21 @@ int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
#endif /* MBEDTLS_CIPHER_MODE_CBC */

CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_enc, key0,
keylen << 3, MBEDTLS_ENCRYPT) == 0);
(keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
MBEDTLS_ENCRYPT)
== 0);
CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_dec, key1,
keylen << 3, MBEDTLS_DECRYPT) == 0);
(keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
MBEDTLS_DECRYPT)
== 0);
CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_enc, key1,
keylen << 3, MBEDTLS_ENCRYPT) == 0);
(keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
MBEDTLS_ENCRYPT)
== 0);
CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_dec, key0,
keylen << 3, MBEDTLS_DECRYPT) == 0);
(keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
MBEDTLS_DECRYPT)
== 0);

/* Setup MAC contexts */
#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Expand Down Expand Up @@ -1154,12 +1163,12 @@ int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
/* Add CID */
memcpy(&t_in->in_cid, cid0, cid0_len);
memcpy(&t_in->out_cid, cid1, cid1_len);
t_in->in_cid_len = cid0_len;
t_in->out_cid_len = cid1_len;
t_in->in_cid_len = (uint8_t) cid0_len;
t_in->out_cid_len = (uint8_t) cid1_len;
memcpy(&t_out->in_cid, cid1, cid1_len);
memcpy(&t_out->out_cid, cid0, cid0_len);
t_out->in_cid_len = cid1_len;
t_out->out_cid_len = cid0_len;
t_out->in_cid_len = (uint8_t) cid1_len;
t_out->out_cid_len = (uint8_t) cid0_len;
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */

cleanup:
Expand Down

0 comments on commit d2696f2

Please sign in to comment.