Rewrite error addition interface

The previous implementation of the error addition interface did not comply
with the invasive testing architecture guidelines. This commit fixes that
by:

- Renaming functions/macros/variables to follow the mbedtls_error_xxx or
  mbedtls_test_hook_xxx convention.

- Making mbedtls_test_hook_error_add a global variable that can be set
  by the testing code.

- Using a static inline function call, as opposed to macro, to keep
  discrepancies between debug and production version to a minimum.

Signed-off-by: Chris Jones <christopher.jones@arm.com>
This commit is contained in:
Chris Jones 2021-04-01 16:00:01 +01:00
parent ac33a3ab12
commit 7439209bcc
5 changed files with 51 additions and 63 deletions

View file

@ -114,25 +114,44 @@ extern "C" {
#define MBEDTLS_ERR_ERROR_GENERIC_ERROR -0x0001 /**< Generic error */
#define MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED -0x006E /**< This is a bug in the library */
#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_ERROR_C)
/**
* \brief Set a function pointer (hook) to allow for invasive testing of error
* code addition.
* \brief Combines a high-level and low-level error code together.
*
* This hook is used in the test infrastructure to report on errors when
* combining two error codes of the same level.
*
* \param hook hook to invasive testing function
* Wrapper function for mbedtls_err_add_ext(). See that function for
* more details.
*/
void mbedtls_set_err_add_hook( void (*hook)( int, int, const char *, int ) );
int mbedtls_err_add( int high, int low, const char *file, int line );
#define MBEDTLS_ERR_ADD( high, low ) \
( mbedtls_err_add( high, low, __FILE__, __LINE__ ) )
#else
#define MBEDTLS_ERR_ADD( high, low ) \
( ( high ) + ( low ) )
#endif /* MBEDTLS_TEST_HOOKS */
#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 );
/**
* \brief Combines a high-level and low-level error code together.
*
* This function can be called directly however it is usually
* called via the mbedtls_error_add macro.
*
* \note When invasive testing is enabled via MBEDTLS_TEST_HOOKS also try to
* call mbedtls_test_hook_error_add.
*
* \param high high-level error code. See error.h for more details.
* \param low low-level error code. See error.h for more details.
* \param file file where this error code addition occured.
* \param line line where this error code addition occured.
*/
static inline int mbedtls_error_add_ext( int high, int low,
const char *file, int line )
{
#if defined(MBEDTLS_TEST_HOOKS)
if( *mbedtls_test_hook_error_add != NULL )
( *mbedtls_test_hook_error_add )( high, low, file, line );
#endif
return( high + low );
}
/**
* \brief Translate a mbed TLS error code into a string representation,