diff --git a/include/mbedtls/error.h b/include/mbedtls/error.h index cff22eaa8..2fb86c7eb 100644 --- a/include/mbedtls/error.h +++ b/include/mbedtls/error.h @@ -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 ); diff --git a/library/error.c b/library/error.c index ee1f8b704..38b658006 100644 --- a/library/error.c +++ b/library/error.c @@ -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) @@ -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) @@ -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 ); @@ -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 ); diff --git a/scripts/data_files/error.fmt b/scripts/data_files/error.fmt index 5699d9ea2..0e128e84f 100644 --- a/scripts/data_files/error.fmt +++ b/scripts/data_files/error.fmt @@ -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 @@ -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 @@ -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 ); @@ -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 );