Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
1. The functions mbedtls_high_level_strerr and mbedtls_low_level_strerr
   accept any error code and extract the high-level and low-level parts
   respectively.
2. Documentation updates.

Signed-off-by: Gaurav Aggarwal <[email protected]>
  • Loading branch information
aggarg committed Apr 20, 2020
1 parent 3d02db2 commit 6ea4fc7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
16 changes: 10 additions & 6 deletions include/mbedtls/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,28 +128,32 @@ extern "C" {
void mbedtls_strerror( int errnum, char *buffer, size_t buflen );

/**
* \brief Translate high level part of a mbed TLS error code into a string
* \brief Translate the high-level part of an Mbed TLS error code into a string
* representation.
*
* This function returns a const pointer to an un-modifiable string. The caller
* must not try to modify the string and use it only for logging purposes.
* must not try to modify the string. It is intended to be used mostly for
* logging purposes.
*
* \param error_code error code
*
* \return The string representation of the error code.
* \return The string representation of the error code, or \c NULL if the error
* code is unknown.
*/
const char * mbedtls_high_level_strerr( int error_code );

/**
* \brief Translate low level part of a mbed TLS error code into a string
* \brief Translate the low-level part of an Mbed TLS error code into a string
* representation.
*
* This function returns a const pointer to an un-modifiable string. The caller
* must not try to modify the string and use it only for logging purposes.
* must not try to modify the string. It is intended to be used mostly for
* logging purposes.
*
* \param error_code error code
*
* \return The string representation of the error code.
* \return The string representation of the error code, or \c NULL if the error
* code is unknown.
*/
const char * mbedtls_low_level_strerr( int error_code );

Expand Down
22 changes: 18 additions & 4 deletions library/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,16 @@

const char * mbedtls_high_level_strerr( int error_code )
{
int high_level_error_code;
const char *error_description = NULL;

switch( error_code )
if( error_code < 0 )
error_code = -error_code;

/* Extract the high-level part from the error code. */
high_level_error_code = error_code & 0xFF80;

switch( high_level_error_code )
{
/* Begin Auto-Generated Code. */
#if defined(MBEDTLS_CIPHER_C)
Expand Down Expand Up @@ -725,9 +732,16 @@ const char * mbedtls_high_level_strerr( int error_code )

const char * mbedtls_low_level_strerr( int error_code )
{
int low_level_error_code;
const char *error_description = NULL;

switch( error_code )
if( error_code < 0 )
error_code = -error_code;

/* Extract the low-level part from the error code. */
low_level_error_code = error_code & ~0xFF80;

switch( low_level_error_code )
{
/* Begin Auto-Generated Code. */
#if defined(MBEDTLS_AES_C)
Expand Down Expand Up @@ -1154,7 +1168,7 @@ void mbedtls_strerror( int ret, char *buf, size_t buflen )
use_ret = ret & 0xFF80;

// Translate high level error code.
high_level_error_description = mbedtls_high_level_strerr(use_ret);
high_level_error_description = mbedtls_high_level_strerr( ret );

if( high_level_error_description == NULL )
mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
Expand Down Expand Up @@ -1191,7 +1205,7 @@ void mbedtls_strerror( int ret, char *buf, size_t buflen )
}

// Translate low level error code.
low_level_error_description = mbedtls_low_level_strerr( use_ret );
low_level_error_description = mbedtls_low_level_strerr( ret );

if( low_level_error_description == NULL )
mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
Expand Down
22 changes: 18 additions & 4 deletions scripts/data_files/error.fmt
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,16 @@ HEADER_INCLUDED

const char * mbedtls_high_level_strerr( int error_code )
{
int high_level_error_code;
const char *error_description = NULL;

switch( error_code )
if( error_code < 0 )
error_code = -error_code;

/* Extract the high-level part from the error code. */
high_level_error_code = error_code & 0xFF80;

switch( high_level_error_code )
{
/* Begin Auto-Generated Code. */
HIGH_LEVEL_CODE_CHECKS
Expand All @@ -61,9 +68,16 @@ HIGH_LEVEL_CODE_CHECKS

const char * mbedtls_low_level_strerr( int error_code )
{
int low_level_error_code;
const char *error_description = NULL;

switch( error_code )
if( error_code < 0 )
error_code = -error_code;

/* Extract the low-level part from the error code. */
low_level_error_code = error_code & ~0xFF80;

switch( low_level_error_code )
{
/* Begin Auto-Generated Code. */
LOW_LEVEL_CODE_CHECKS
Expand Down Expand Up @@ -96,7 +110,7 @@ void mbedtls_strerror( int ret, char *buf, size_t buflen )
use_ret = ret & 0xFF80;

// Translate high level error code.
high_level_error_description = mbedtls_high_level_strerr(use_ret);
high_level_error_description = mbedtls_high_level_strerr( ret );

if( high_level_error_description == NULL )
mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
Expand Down Expand Up @@ -133,7 +147,7 @@ void mbedtls_strerror( int ret, char *buf, size_t buflen )
}

// Translate low level error code.
low_level_error_description = mbedtls_low_level_strerr( use_ret );
low_level_error_description = mbedtls_low_level_strerr( ret );

if( low_level_error_description == NULL )
mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
Expand Down

0 comments on commit 6ea4fc7

Please sign in to comment.