From ac5361f7dc85346064991bd797f6a3378ffb87a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 24 Jun 2015 01:08:09 +0200 Subject: [PATCH] Fix small issues in tests found by Coverity --- tests/suites/test_suite_cipher.function | 33 +++++++++++++--------- tests/suites/test_suite_md.function | 2 +- tests/suites/test_suite_mpi.function | 7 +++-- tests/suites/test_suite_pkcs5.function | 2 -- tests/suites/test_suite_x509write.function | 2 +- 5 files changed, 27 insertions(+), 19 deletions(-) diff --git a/tests/suites/test_suite_cipher.function b/tests/suites/test_suite_cipher.function index 30f154c62..967a6a0a1 100644 --- a/tests/suites/test_suite_cipher.function +++ b/tests/suites/test_suite_cipher.function @@ -95,7 +95,7 @@ void cipher_null_args( ) void enc_dec_buf( int cipher_id, char *cipher_string, int key_len, int length_val, int pad_mode ) { - size_t length = length_val, outlen, total_len, i; + size_t length = length_val, outlen, total_len, i, block_size; unsigned char key[32]; unsigned char iv[16]; unsigned char ad[13]; @@ -162,14 +162,17 @@ void enc_dec_buf( int cipher_id, char *cipher_string, int key_len, TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) ); #endif + block_size = mbedtls_cipher_get_block_size( &ctx_enc ); + TEST_ASSERT( block_size != 0 ); + /* encode length number of bytes from inbuf */ TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); total_len = outlen; TEST_ASSERT( total_len == length || - ( total_len % mbedtls_cipher_get_block_size( &ctx_enc ) == 0 && + ( total_len % block_size == 0 && total_len < length && - total_len + mbedtls_cipher_get_block_size( &ctx_enc ) > length ) ); + total_len + block_size > length ) ); TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); total_len += outlen; @@ -179,18 +182,18 @@ void enc_dec_buf( int cipher_id, char *cipher_string, int key_len, #endif TEST_ASSERT( total_len == length || - ( total_len % mbedtls_cipher_get_block_size( &ctx_enc ) == 0 && + ( total_len % block_size == 0 && total_len > length && - total_len <= length + mbedtls_cipher_get_block_size( &ctx_enc ) ) ); + total_len <= length + block_size ) ); /* decode the previously encoded string */ TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) ); total_len = outlen; TEST_ASSERT( total_len == length || - ( total_len % mbedtls_cipher_get_block_size( &ctx_dec ) == 0 && + ( total_len % block_size == 0 && total_len < length && - total_len + mbedtls_cipher_get_block_size( &ctx_dec ) >= length ) ); + total_len + block_size >= length ) ); TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); total_len += outlen; @@ -322,6 +325,7 @@ void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val, size_t first_length = first_length_val; size_t second_length = second_length_val; size_t length = first_length + second_length; + size_t block_size; unsigned char key[32]; unsigned char iv[16]; @@ -367,31 +371,34 @@ void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val, TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, NULL, 0 ) ); #endif + block_size = mbedtls_cipher_get_block_size( &ctx_enc ); + TEST_ASSERT( block_size != 0 ); + /* encode length number of bytes from inbuf */ TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); totaloutlen = outlen; TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); totaloutlen += outlen; TEST_ASSERT( totaloutlen == length || - ( totaloutlen % mbedtls_cipher_get_block_size( &ctx_enc ) == 0 && + ( totaloutlen % block_size == 0 && totaloutlen < length && - totaloutlen + mbedtls_cipher_get_block_size( &ctx_enc ) > length ) ); + totaloutlen + block_size > length ) ); TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); totaloutlen += outlen; TEST_ASSERT( totaloutlen == length || - ( totaloutlen % mbedtls_cipher_get_block_size( &ctx_enc ) == 0 && + ( totaloutlen % block_size == 0 && totaloutlen > length && - totaloutlen <= length + mbedtls_cipher_get_block_size( &ctx_enc ) ) ); + totaloutlen <= length + block_size ) ); /* decode the previously encoded string */ TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) ); totaloutlen = outlen; TEST_ASSERT( totaloutlen == length || - ( totaloutlen % mbedtls_cipher_get_block_size( &ctx_dec ) == 0 && + ( totaloutlen % block_size == 0 && totaloutlen < length && - totaloutlen + mbedtls_cipher_get_block_size( &ctx_dec ) >= length ) ); + totaloutlen + block_size >= length ) ); TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); totaloutlen += outlen; diff --git a/tests/suites/test_suite_md.function b/tests/suites/test_suite_md.function index 099d109a2..0fcaa388f 100644 --- a/tests/suites/test_suite_md.function +++ b/tests/suites/test_suite_md.function @@ -359,7 +359,7 @@ void mbedtls_md_file( char *text_md_name, char *filename, char *hex_hash_string md_info = mbedtls_md_info_from_string( md_name ); TEST_ASSERT( md_info != NULL ); - mbedtls_md_file( md_info, filename, output); + TEST_ASSERT( mbedtls_md_file( md_info, filename, output ) == 0 ); hexify( hash_str, output, mbedtls_md_get_size(md_info) ); TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 ); diff --git a/tests/suites/test_suite_mpi.function b/tests/suites/test_suite_mpi.function index 1308e56aa..72b49408c 100644 --- a/tests/suites/test_suite_mpi.function +++ b/tests/suites/test_suite_mpi.function @@ -150,6 +150,7 @@ void mbedtls_mpi_write_file( int radix_X, char *input_X, int output_radix, { mbedtls_mpi X, Y; FILE *file_out, *file_in; + int ret; mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); @@ -157,13 +158,15 @@ void mbedtls_mpi_write_file( int radix_X, char *input_X, int output_radix, file_out = fopen( output_file, "w" ); TEST_ASSERT( file_out != NULL ); - TEST_ASSERT( mbedtls_mpi_write_file( NULL, &X, output_radix, file_out ) == 0 ); + ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out ); fclose(file_out); + TEST_ASSERT( ret == 0 ); file_in = fopen( output_file, "r" ); TEST_ASSERT( file_in != NULL ); - TEST_ASSERT( mbedtls_mpi_read_file( &Y, output_radix, file_in ) == 0 ); + ret = mbedtls_mpi_read_file( &Y, output_radix, file_in ); fclose(file_in); + TEST_ASSERT( ret == 0 ); TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 ); diff --git a/tests/suites/test_suite_pkcs5.function b/tests/suites/test_suite_pkcs5.function index 41531ad54..ab53326d4 100644 --- a/tests/suites/test_suite_pkcs5.function +++ b/tests/suites/test_suite_pkcs5.function @@ -34,8 +34,6 @@ void pbkdf2_hmac( int hash, char *hex_password_string, info = mbedtls_md_info_from_type( hash ); TEST_ASSERT( info != NULL ); - if( info == NULL ) - return; TEST_ASSERT( mbedtls_md_setup( &ctx, info, 1 ) == 0 ); TEST_ASSERT( mbedtls_pkcs5_pbkdf2_hmac( &ctx, pw_str, pw_len, salt_str, salt_len, it_cnt, key_len, key ) == 0 ); diff --git a/tests/suites/test_suite_x509write.function b/tests/suites/test_suite_x509write.function index 789ddbc64..356af7524 100644 --- a/tests/suites/test_suite_x509write.function +++ b/tests/suites/test_suite_x509write.function @@ -119,8 +119,8 @@ void x509_crt_check( char *subject_key_file, char *subject_pwd, f = fopen( cert_check_file, "r" ); TEST_ASSERT( f != NULL ); olen = fread( check_buf, 1, sizeof(check_buf), f ); - TEST_ASSERT( olen < sizeof(check_buf) ); fclose( f ); + TEST_ASSERT( olen < sizeof(check_buf) ); TEST_ASSERT( olen >= pem_len - 1 ); TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 );