Move the ARRAY_LENGTH definition to common.h
Reuse it in the library and tests. Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
This commit is contained in:
parent
1c7a99856f
commit
b22b9778c7
7 changed files with 39 additions and 47 deletions
|
@ -65,6 +65,44 @@ extern void (*mbedtls_test_hook_test_fail)(const char *test, int line, const cha
|
||||||
#define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST)
|
#define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST)
|
||||||
#endif /* defined(MBEDTLS_TEST_HOOKS) */
|
#endif /* defined(MBEDTLS_TEST_HOOKS) */
|
||||||
|
|
||||||
|
/** \def ARRAY_LENGTH
|
||||||
|
* Return the number of elements of a static or stack array.
|
||||||
|
*
|
||||||
|
* \param array A value of array (not pointer) type.
|
||||||
|
*
|
||||||
|
* \return The number of elements of the array.
|
||||||
|
*/
|
||||||
|
/* A correct implementation of ARRAY_LENGTH, but which silently gives
|
||||||
|
* a nonsensical result if called with a pointer rather than an array. */
|
||||||
|
#define ARRAY_LENGTH_UNSAFE(array) \
|
||||||
|
(sizeof(array) / sizeof(*(array)))
|
||||||
|
|
||||||
|
#if defined(__GNUC__)
|
||||||
|
/* Test if arg and &(arg)[0] have the same type. This is true if arg is
|
||||||
|
* an array but not if it's a pointer. */
|
||||||
|
#define IS_ARRAY_NOT_POINTER(arg) \
|
||||||
|
(!__builtin_types_compatible_p(__typeof__(arg), \
|
||||||
|
__typeof__(&(arg)[0])))
|
||||||
|
/* A compile-time constant with the value 0. If `const_expr` is not a
|
||||||
|
* compile-time constant with a nonzero value, cause a compile-time error. */
|
||||||
|
#define STATIC_ASSERT_EXPR(const_expr) \
|
||||||
|
(0 && sizeof(struct { unsigned int STATIC_ASSERT : 1 - 2 * !(const_expr); }))
|
||||||
|
|
||||||
|
/* Return the scalar value `value` (possibly promoted). This is a compile-time
|
||||||
|
* constant if `value` is. `condition` must be a compile-time constant.
|
||||||
|
* If `condition` is false, arrange to cause a compile-time error. */
|
||||||
|
#define STATIC_ASSERT_THEN_RETURN(condition, value) \
|
||||||
|
(STATIC_ASSERT_EXPR(condition) ? 0 : (value))
|
||||||
|
|
||||||
|
#define ARRAY_LENGTH(array) \
|
||||||
|
(STATIC_ASSERT_THEN_RETURN(IS_ARRAY_NOT_POINTER(array), \
|
||||||
|
ARRAY_LENGTH_UNSAFE(array)))
|
||||||
|
|
||||||
|
#else
|
||||||
|
/* If we aren't sure the compiler supports our non-standard tricks,
|
||||||
|
* fall back to the unsafe implementation. */
|
||||||
|
#define ARRAY_LENGTH(array) ARRAY_LENGTH_UNSAFE(array)
|
||||||
|
#endif
|
||||||
/** Allow library to access its structs' private members.
|
/** Allow library to access its structs' private members.
|
||||||
*
|
*
|
||||||
* Although structs defined in header files are publicly available,
|
* Although structs defined in header files are publicly available,
|
||||||
|
|
|
@ -84,8 +84,6 @@
|
||||||
#include "mbedtls/sha512.h"
|
#include "mbedtls/sha512.h"
|
||||||
#include "md_psa.h"
|
#include "md_psa.h"
|
||||||
|
|
||||||
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
|
||||||
defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
|
defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
|
||||||
defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
|
defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
|
||||||
|
|
|
@ -36,8 +36,6 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "mbedtls/platform.h"
|
#include "mbedtls/platform.h"
|
||||||
|
|
||||||
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
|
psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
|
||||||
unsigned key_slots_initialized : 1;
|
unsigned key_slots_initialized : 1;
|
||||||
|
|
|
@ -1001,8 +1001,6 @@ static sha_test_sum_t sha512_test_sum[] =
|
||||||
};
|
};
|
||||||
#endif /* MBEDTLS_SHA512_C */
|
#endif /* MBEDTLS_SHA512_C */
|
||||||
|
|
||||||
#define ARRAY_LENGTH(a) (sizeof(a) / sizeof((a)[0]))
|
|
||||||
|
|
||||||
static int mbedtls_sha512_common_self_test(int verbose, int is384)
|
static int mbedtls_sha512_common_self_test(int verbose, int is384)
|
||||||
{
|
{
|
||||||
int i, buflen, ret = 0;
|
int i, buflen, ret = 0;
|
||||||
|
|
|
@ -759,8 +759,6 @@ void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
|
||||||
#define ARRAY_LENGTH(a) (sizeof(a) / sizeof(*(a)))
|
|
||||||
|
|
||||||
static const char *ticket_flag_name_table[] =
|
static const char *ticket_flag_name_table[] =
|
||||||
{
|
{
|
||||||
[0] = "ALLOW_PSK_RESUMPTION",
|
[0] = "ALLOW_PSK_RESUMPTION",
|
||||||
|
|
|
@ -196,45 +196,6 @@
|
||||||
mbedtls_exit(1); \
|
mbedtls_exit(1); \
|
||||||
}
|
}
|
||||||
|
|
||||||
/** \def ARRAY_LENGTH
|
|
||||||
* Return the number of elements of a static or stack array.
|
|
||||||
*
|
|
||||||
* \param array A value of array (not pointer) type.
|
|
||||||
*
|
|
||||||
* \return The number of elements of the array.
|
|
||||||
*/
|
|
||||||
/* A correct implementation of ARRAY_LENGTH, but which silently gives
|
|
||||||
* a nonsensical result if called with a pointer rather than an array. */
|
|
||||||
#define ARRAY_LENGTH_UNSAFE(array) \
|
|
||||||
(sizeof(array) / sizeof(*(array)))
|
|
||||||
|
|
||||||
#if defined(__GNUC__)
|
|
||||||
/* Test if arg and &(arg)[0] have the same type. This is true if arg is
|
|
||||||
* an array but not if it's a pointer. */
|
|
||||||
#define IS_ARRAY_NOT_POINTER(arg) \
|
|
||||||
(!__builtin_types_compatible_p(__typeof__(arg), \
|
|
||||||
__typeof__(&(arg)[0])))
|
|
||||||
/* A compile-time constant with the value 0. If `const_expr` is not a
|
|
||||||
* compile-time constant with a nonzero value, cause a compile-time error. */
|
|
||||||
#define STATIC_ASSERT_EXPR(const_expr) \
|
|
||||||
(0 && sizeof(struct { unsigned int STATIC_ASSERT : 1 - 2 * !(const_expr); }))
|
|
||||||
|
|
||||||
/* Return the scalar value `value` (possibly promoted). This is a compile-time
|
|
||||||
* constant if `value` is. `condition` must be a compile-time constant.
|
|
||||||
* If `condition` is false, arrange to cause a compile-time error. */
|
|
||||||
#define STATIC_ASSERT_THEN_RETURN(condition, value) \
|
|
||||||
(STATIC_ASSERT_EXPR(condition) ? 0 : (value))
|
|
||||||
|
|
||||||
#define ARRAY_LENGTH(array) \
|
|
||||||
(STATIC_ASSERT_THEN_RETURN(IS_ARRAY_NOT_POINTER(array), \
|
|
||||||
ARRAY_LENGTH_UNSAFE(array)))
|
|
||||||
|
|
||||||
#else
|
|
||||||
/* If we aren't sure the compiler supports our non-standard tricks,
|
|
||||||
* fall back to the unsafe implementation. */
|
|
||||||
#define ARRAY_LENGTH(array) ARRAY_LENGTH_UNSAFE(array)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/** Return the smaller of two values.
|
/** Return the smaller of two values.
|
||||||
*
|
*
|
||||||
* \param x An integer-valued expression without side effects.
|
* \param x An integer-valued expression without side effects.
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
#include <test/macros.h>
|
#include <test/macros.h>
|
||||||
#include <psa_crypto_slot_management.h>
|
#include <psa_crypto_slot_management.h>
|
||||||
#include <test/psa_crypto_helpers.h>
|
#include <test/psa_crypto_helpers.h>
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue