From c57d0e5ed5a5560bcee2775d03ce855f6abf7e7c Mon Sep 17 00:00:00 2001 From: Azim Khan Date: Wed, 23 May 2018 16:55:16 +0100 Subject: [PATCH 1/3] Treat warnings as errors for IAR Fix IAR compiler warnings Two warnings have been fixed: 1. code 'if( len <= 0xFFFFFFFF )' gave warning 'pointless integer comparison'. This was fixed by wraping the condition in '#if SIZE_MAX > 0xFFFFFFFF'. 2. code 'diff |= A[i] ^ B[i];' gave warning 'the order of volatile accesses is undefined in'. This was fixed by read the volatile data in temporary variables before the computation. Explain IAR warning on volatile access Consistent use of CMAKE_C_COMPILER_ID --- CMakeLists.txt | 23 ++++++++++++++++------- include/mbedtls/ssl_internal.h | 8 +++++++- library/pkcs5.c | 2 ++ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ecacc7ade..b61350eca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,8 +7,13 @@ option(ENABLE_ZLIB_SUPPORT "Build mbed TLS with zlib library." OFF) option(ENABLE_PROGRAMS "Build mbed TLS programs." ON) +string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}") +string(REGEX MATCH "GNU" CMAKE_COMPILER_IS_GNU "${CMAKE_C_COMPILER_ID}") +string(REGEX MATCH "IAR" CMAKE_COMPILER_IS_IAR "${CMAKE_C_COMPILER_ID}") +string(REGEX MATCH "MSVC" CMAKE_COMPILER_IS_MSVC "${CMAKE_C_COMPILER_ID}") + # the test suites currently have compile errors with MSVC -if(MSVC) +if(CMAKE_COMPILER_IS_MSVC) option(ENABLE_TESTING "Build mbed TLS tests." OFF) else() option(ENABLE_TESTING "Build mbed TLS tests." ON) @@ -48,7 +53,7 @@ endfunction(link_to_source) string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}") -if(CMAKE_COMPILER_IS_GNUCC) +if(CMAKE_COMPILER_IS_GNU) # some warnings we want are not available with old GCC versions # note: starting with CMake 2.8 we could use CMAKE_C_COMPILER_VERSION execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion @@ -67,7 +72,7 @@ if(CMAKE_COMPILER_IS_GNUCC) set(CMAKE_C_FLAGS_ASANDBG "-Werror -fsanitize=address -fno-common -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls ") set(CMAKE_C_FLAGS_CHECK "-Werror -Os") set(CMAKE_C_FLAGS_CHECKFULL "${CMAKE_C_FLAGS_CHECK} -Wcast-qual") -endif(CMAKE_COMPILER_IS_GNUCC) +endif(CMAKE_COMPILER_IS_GNU) if(CMAKE_COMPILER_IS_CLANG) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -W -Wdeclaration-after-statement -Wwrite-strings -Wpointer-arith -Wimplicit-fallthrough -Wshadow") @@ -81,16 +86,20 @@ if(CMAKE_COMPILER_IS_CLANG) set(CMAKE_C_FLAGS_CHECK "-Werror -Os") endif(CMAKE_COMPILER_IS_CLANG) -if(MSVC) +if(CMAKE_COMPILER_IS_IAR) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --warn_about_c_style_casts --warnings_are_errors -Ohz") +endif(CMAKE_COMPILER_IS_IAR) + +if(CMAKE_COMPILER_IS_MSVC) # Strictest warnings, and treat as errors set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX") -endif(MSVC) +endif(CMAKE_COMPILER_IS_MSVC) if(CMAKE_BUILD_TYPE STREQUAL "Coverage") - if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG) + if(CMAKE_COMPILER_IS_GNU OR CMAKE_COMPILER_IS_CLANG) set(CMAKE_SHARED_LINKER_FLAGS "--coverage") - endif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG) + endif(CMAKE_COMPILER_IS_GNU OR CMAKE_COMPILER_IS_CLANG) endif(CMAKE_BUILD_TYPE STREQUAL "Coverage") if(LIB_INSTALL_DIR) diff --git a/include/mbedtls/ssl_internal.h b/include/mbedtls/ssl_internal.h index cc0e7865c..e4b609c9a 100644 --- a/include/mbedtls/ssl_internal.h +++ b/include/mbedtls/ssl_internal.h @@ -613,7 +613,13 @@ static inline int mbedtls_ssl_safer_memcmp( const void *a, const void *b, size_t volatile unsigned char diff = 0; for( i = 0; i < n; i++ ) - diff |= A[i] ^ B[i]; + { + /* Read volatile data in order before computing diff. + * This avoids IAR compiler warning: + * 'the order of volatile accesses is undefined ..' */ + unsigned char x = A[i], y = B[i]; + diff |= x ^ y; + } return( diff ); } diff --git a/library/pkcs5.c b/library/pkcs5.c index 35146b74b..a20471084 100644 --- a/library/pkcs5.c +++ b/library/pkcs5.c @@ -249,8 +249,10 @@ int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx, const unsigned char *p memset( counter, 0, 4 ); counter[3] = 1; +#if UINT_MAX > 0xFFFFFFFF if( iteration_count > 0xFFFFFFFF ) return( MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA ); +#endif while( key_length ) { From b5c87012ec958dff4b37192b70a195344cd00e29 Mon Sep 17 00:00:00 2001 From: Andres Amaya Garcia Date: Tue, 27 Jun 2017 16:15:06 +0100 Subject: [PATCH 2/3] Initialise pointers to avoid IAR compiler warnings --- library/ssl_cli.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/ssl_cli.c b/library/ssl_cli.c index 2d1dcf868..a57d866f3 100644 --- a/library/ssl_cli.c +++ b/library/ssl_cli.c @@ -2086,7 +2086,7 @@ static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl ) int ret; const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info; - unsigned char *p, *end; + unsigned char *p = NULL, *end = NULL; MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) ); From 577d39b93064cff1feeffe0ed26faca275ce1bd0 Mon Sep 17 00:00:00 2001 From: Simon Butcher Date: Thu, 14 Jun 2018 08:58:59 +0100 Subject: [PATCH 3/3] Compilation warning fixes on 32b platfrom with IAR Fix compilation warnings with IAR toolchain, on 32 bit platform. Reported by rahmanih in #683 This is based on work by Ron Eldor in PR #750. --- ChangeLog | 3 ++- library/ssl_srv.c | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 93d017c6c..838238b15 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,7 +3,8 @@ mbed TLS ChangeLog (Sorted per branch, date) = mbed TLS x.x.x branch released xxxx-xx-xx Bugfix - + * Fix compilation warnings with IAR toolchain, on 32 bit platform. + Reported by rahmanih in #683 * Fix braces in mbedtls_memory_buffer_alloc_status(). Found by sbranden, #552. * Added the macro MBEDTLS_X509_MAX_FILE_PATH_LEN that enables the user to configure the maximum length of a file path that can be buffered when diff --git a/library/ssl_srv.c b/library/ssl_srv.c index af3722cec..9a884f055 100644 --- a/library/ssl_srv.c +++ b/library/ssl_srv.c @@ -2686,7 +2686,7 @@ static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl ) #if defined(MBEDTLS_KEY_EXCHANGE__SOME_PFS__ENABLED) unsigned char *p = ssl->out_msg + 4; - size_t len; + size_t len = 0; #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) unsigned char *dig_signed = p; size_t dig_signed_len = 0;