From 7e1e7be8fcd67884b881aaa1760914566e8356a5 Mon Sep 17 00:00:00 2001 From: Dave Rodgman Date: Tue, 5 Sep 2023 18:12:33 +0100 Subject: [PATCH] Simplify fixes for unreachable code Signed-off-by: Dave Rodgman --- library/bignum_core.c | 35 +++++++---------------------------- library/common.h | 19 ------------------- 2 files changed, 7 insertions(+), 47 deletions(-) diff --git a/library/bignum_core.c b/library/bignum_core.c index 85dca5530..441151e66 100644 --- a/library/bignum_core.c +++ b/library/bignum_core.c @@ -77,40 +77,19 @@ size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs) return 0; } -/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint - * into the storage form used by mbedtls_mpi. */ -static mbedtls_mpi_uint mpi_bigendian_to_host_c(mbedtls_mpi_uint a) -{ - uint8_t i; - unsigned char *a_ptr; - mbedtls_mpi_uint tmp = 0; - - for (i = 0, a_ptr = (unsigned char *) &a; i < ciL; i++, a_ptr++) { - tmp <<= CHAR_BIT; - tmp |= (mbedtls_mpi_uint) *a_ptr; - } - - return tmp; -} - static mbedtls_mpi_uint mpi_bigendian_to_host(mbedtls_mpi_uint a) { if (MBEDTLS_IS_BIG_ENDIAN) { /* Nothing to do on bigendian systems. */ return a; } else { -MBEDTLS_IGNORE_UNREACHABLE_BEGIN - switch (sizeof(mbedtls_mpi_uint)) { - case 4: - return (mbedtls_mpi_uint) MBEDTLS_BSWAP32((uint32_t) a); - case 8: - return (mbedtls_mpi_uint) MBEDTLS_BSWAP64((uint64_t) a); - } - - /* Fall back to C-based reordering if we don't know the byte order - * or we couldn't use a compiler-specific builtin. */ - return mpi_bigendian_to_host_c(a); -MBEDTLS_IGNORE_UNREACHABLE_END +#if defined(MBEDTLS_HAVE_INT32) + return (mbedtls_mpi_uint) MBEDTLS_BSWAP32((uint32_t) a); +#elif defined(MBEDTLS_HAVE_INT64) + return (mbedtls_mpi_uint) MBEDTLS_BSWAP64((uint64_t) a); +#else +#error "This is one of several places that need to be adapted to support a new limb size" +#endif } } diff --git a/library/common.h b/library/common.h index 4e1b2fa2d..3c472c685 100644 --- a/library/common.h +++ b/library/common.h @@ -334,23 +334,4 @@ static inline void mbedtls_xor_no_simd(unsigned char *r, #define MBEDTLS_OPTIMIZE_FOR_PERFORMANCE #endif -/* Define macros that can be used to disable warnings about unreachable code. */ -#if defined(__clang__) - -#define MBEDTLS_PRAGMA(x) _Pragma(#x) - -#define MBEDTLS_IGNORE_UNREACHABLE_BEGIN \ - MBEDTLS_PRAGMA(clang diagnostic push) \ - MBEDTLS_PRAGMA(clang diagnostic ignored "-Wunreachable-code") - -#define MBEDTLS_IGNORE_UNREACHABLE_END \ - MBEDTLS_PRAGMA(clang diagnostic pop) - -#else - -#define MBEDTLS_IGNORE_UNREACHABLE_BEGIN -#define MBEDTLS_IGNORE_UNREACHABLE_END - -#endif - #endif /* MBEDTLS_LIBRARY_COMMON_H */