Address review comments

- Use switch case instead of loop to generate faster code
- Add #if defined to address compiler error

Signed-off-by: Gaurav Aggarwal <aggarg@amazon.com>
This commit is contained in:
Gaurav Aggarwal 2020-04-09 11:39:04 -07:00
parent a9f64006ea
commit a4a2aa5169
3 changed files with 884 additions and 420 deletions

File diff suppressed because it is too large Load diff

View file

@ -42,56 +42,32 @@
HEADER_INCLUDED HEADER_INCLUDED
typedef struct mbedtls_error
{
int code; /* Error code. */
const char * description; /* Error description. */
} mbedtls_error_t;
static mbedtls_error_t high_level_errors[] =
{
HIGH_LEVEL_CODE_CHECKS
};
#define NUM_HIGH_LEVEL_ERRORS ( sizeof(high_level_errors)/sizeof(mbedtls_error_t) )
static mbedtls_error_t low_level_errors[] =
{
LOW_LEVEL_CODE_CHECKS
};
#define NUM_LOW_LEVEL_ERRORS ( sizeof(low_level_errors)/sizeof(mbedtls_error_t) )
const char * mbedtls_high_level_strerr( int error_code ) const char * mbedtls_high_level_strerr( int error_code )
{ {
size_t i;
const char *error_description = NULL; const char *error_description = NULL;
for(i = 0; i < NUM_HIGH_LEVEL_ERRORS; i++ ) switch( error_code )
{ {
if( high_level_errors[i].code == error_code ) HIGH_LEVEL_CODE_CHECKS
{
error_description = high_level_errors[i].description; default:
break; break;
} }
}
return error_description; return error_description;
} }
const char * mbedtls_low_level_strerr( int error_code ) const char * mbedtls_low_level_strerr( int error_code )
{ {
size_t i;
const char *error_description = NULL; const char *error_description = NULL;
for(i = 0; i < NUM_LOW_LEVEL_ERRORS; i++ ) switch( error_code )
{ {
if( low_level_errors[i].code == error_code ) LOW_LEVEL_CODE_CHECKS
{
error_description = low_level_errors[i].description; default:
break; break;
} }
}
return error_description; return error_description;
} }
@ -123,10 +99,12 @@ void mbedtls_strerror( int ret, char *buf, size_t buflen )
else else
mbedtls_snprintf( buf, buflen, "%s", high_level_error_description ); mbedtls_snprintf( buf, buflen, "%s", high_level_error_description );
#if defined(MBEDTLS_SSL_TLS_C)
// Early return in case of a fatal error - do not try to translate low // Early return in case of a fatal error - do not try to translate low
// level code. // level code.
if(use_ret == -(MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE)) if(use_ret == -(MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE))
return; return;
#endif /* MBEDTLS_SSL_TLS_C */
} }
use_ret = ret & ~0xFF80; use_ret = ret & ~0xFF80;

View file

@ -160,7 +160,9 @@ foreach my $line (@matches)
${$old_define} = $define_name; ${$old_define} = $define_name;
} }
${$code_check} .= "${white_space}\{.code = -($error_name), .description=\"$module_name - $description\"},\n"; ${$code_check} .= "${white_space}case -($error_name):\n".
"${white_space} error_description = \"$module_name - $description\";\n".
"${white_space} break;\n"
}; };
if ($ll_old_define ne "") if ($ll_old_define ne "")