From b7d02e0f15469237073aeaf4fe71e50ad1b0543b Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Thu, 1 Apr 2021 17:40:03 +0100 Subject: [PATCH] Fix misc issues with unused parameters and check-names.sh Fix unused parameter warnings when MBEDTLS_TEST_HOOKS is not enabled. A few issues were caught by check-names.sh namely: - mbedtls_error_add was not capitalised. - mbedtls_test_hook_error_add was being defined multiple times as the definition was in a header. Signed-off-by: Chris Jones --- include/mbedtls/error.h | 7 +++++-- library/error.c | 2 ++ library/rsa.c | 28 ++++++++++++++-------------- scripts/data_files/error.fmt | 2 ++ 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/include/mbedtls/error.h b/include/mbedtls/error.h index 5b31b61be..39874b9c4 100644 --- a/include/mbedtls/error.h +++ b/include/mbedtls/error.h @@ -120,14 +120,14 @@ extern "C" { * Wrapper function for mbedtls_err_add_ext(). See that function for * more details. */ -#define mbedtls_error_add( high, low ) \ +#define MBEDTLS_ERROR_ADD( high, low ) \ mbedtls_error_add_ext( high, low, __FILE__, __LINE__ ) /** * \brief Testing hook called before adding/combining two error codes together. * Only used when invasive testing is enabled via MBEDTLS_TEST_HOOKS. */ -void (*mbedtls_test_hook_error_add)( int, int, const char *, int ); +extern void (*mbedtls_test_hook_error_add)( int, int, const char *, int ); /** * \brief Combines a high-level and low-level error code together. @@ -150,6 +150,9 @@ static inline int mbedtls_error_add_ext( int high, int low, if( *mbedtls_test_hook_error_add != NULL ) ( *mbedtls_test_hook_error_add )( high, low, file, line ); #endif + (void)file; + (void)line; + return( high + low ); } diff --git a/library/error.c b/library/error.c index 901a3699a..b5bd8d77c 100644 --- a/library/error.c +++ b/library/error.c @@ -893,6 +893,8 @@ const char * mbedtls_low_level_strerr( int error_code ) return( NULL ); } +void (*mbedtls_test_hook_error_add)( int, int, const char *, int ); + void mbedtls_strerror( int ret, char *buf, size_t buflen ) { size_t len; diff --git a/library/rsa.c b/library/rsa.c index 42b43ca4d..268d025e6 100644 --- a/library/rsa.c +++ b/library/rsa.c @@ -102,7 +102,7 @@ int mbedtls_rsa_import( mbedtls_rsa_context *ctx, ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) || ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) ) { - return( mbedtls_error_add( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) ); + return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) ); } if( N != NULL ) @@ -142,7 +142,7 @@ int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx, cleanup: if( ret != 0 ) - return( mbedtls_error_add( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) ); + return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) ); return( 0 ); } @@ -293,7 +293,7 @@ int mbedtls_rsa_complete( mbedtls_rsa_context *ctx ) if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) ) != 0 ) { - return( mbedtls_error_add( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) ); + return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) ); } ctx->len = mbedtls_mpi_size( &ctx->N ); @@ -308,7 +308,7 @@ int mbedtls_rsa_complete( mbedtls_rsa_context *ctx ) ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D, &ctx->P, &ctx->Q ); if( ret != 0 ) - return( mbedtls_error_add( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) ); + return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) ); } else if( d_missing ) @@ -318,7 +318,7 @@ int mbedtls_rsa_complete( mbedtls_rsa_context *ctx ) &ctx->E, &ctx->D ) ) != 0 ) { - return( mbedtls_error_add( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) ); + return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) ); } } @@ -333,7 +333,7 @@ int mbedtls_rsa_complete( mbedtls_rsa_context *ctx ) ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D, &ctx->DP, &ctx->DQ, &ctx->QP ); if( ret != 0 ) - return( mbedtls_error_add( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) ); + return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) ); } #endif /* MBEDTLS_RSA_NO_CRT */ @@ -461,13 +461,13 @@ int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx, ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) || ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) ) { - return( mbedtls_error_add( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) ); + return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) ); } #else if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D, DP, DQ, QP ) ) != 0 ) { - return( mbedtls_error_add( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) ); + return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) ); } #endif @@ -637,7 +637,7 @@ cleanup: mbedtls_rsa_free( ctx ); if( ( -ret & ~0x7f ) == 0 ) - ret = mbedtls_error_add( MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret ); + ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret ); return( ret ); } @@ -770,7 +770,7 @@ cleanup: mbedtls_mpi_free( &T ); if( ret != 0 ) - return( mbedtls_error_add( MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret ) ); + return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret ) ); return( 0 ); } @@ -1086,7 +1086,7 @@ cleanup: mbedtls_mpi_free( &I ); if( ret != 0 && ret >= -0x007f ) - return( mbedtls_error_add( MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret ) ); + return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret ) ); return( ret ); } @@ -1199,7 +1199,7 @@ int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx, /* Generate a random octet string seed */ if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 ) - return( mbedtls_error_add( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) ); + return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) ); p += hlen; @@ -1288,7 +1288,7 @@ int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx, /* Check if RNG failed to generate data */ if( rng_dl == 0 || ret != 0 ) - return( mbedtls_error_add( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) ); + return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) ); p++; } @@ -1882,7 +1882,7 @@ static int rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx, /* Generate salt of length slen in place in the encoded message */ salt = p; if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 ) - return( mbedtls_error_add( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) ); + return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) ); p += slen; diff --git a/scripts/data_files/error.fmt b/scripts/data_files/error.fmt index 9e479bbfd..7fed598ea 100644 --- a/scripts/data_files/error.fmt +++ b/scripts/data_files/error.fmt @@ -82,6 +82,8 @@ LOW_LEVEL_CODE_CHECKS return( NULL ); } +void (*mbedtls_test_hook_error_add)( int, int, const char *, int ); + void mbedtls_strerror( int ret, char *buf, size_t buflen ) { size_t len;