Minor optimizations (original by Peter Vaskovic, modified by Paul Bakker)

Move strlen out of for loop.
Remove redundant null checks before free.
This commit is contained in:
Paul Bakker 2014-05-28 11:33:54 +02:00
parent 8ebfe084ab
commit 14b16c62e9
4 changed files with 9 additions and 12 deletions

View File

@ -370,10 +370,7 @@ int pem_read_buffer( pem_context *ctx, const char *header, const char *footer,
void pem_free( pem_context *ctx ) void pem_free( pem_context *ctx )
{ {
if( ctx->buf )
polarssl_free( ctx->buf ); polarssl_free( ctx->buf );
if( ctx->info )
polarssl_free( ctx->info ); polarssl_free( ctx->info );
memset( ctx, 0, sizeof( pem_context ) ); memset( ctx, 0, sizeof( pem_context ) );

View File

@ -321,7 +321,6 @@ void ssl_cache_free( ssl_cache_context *cache )
ssl_session_free( &prv->session ); ssl_session_free( &prv->session );
#if defined(POLARSSL_X509_CRT_PARSE_C) #if defined(POLARSSL_X509_CRT_PARSE_C)
if( prv->peer_cert.p != NULL )
polarssl_free( prv->peer_cert.p ); polarssl_free( prv->peer_cert.p );
#endif /* POLARSSL_X509_CRT_PARSE_C */ #endif /* POLARSSL_X509_CRT_PARSE_C */

View File

@ -691,7 +691,7 @@ static int ssl_parse_session_ticket_ext( ssl_context *ssl,
static int ssl_parse_alpn_ext( ssl_context *ssl, static int ssl_parse_alpn_ext( ssl_context *ssl,
unsigned char *buf, size_t len ) unsigned char *buf, size_t len )
{ {
size_t list_len, cur_len; size_t list_len, cur_len, ours_len;
const unsigned char *theirs, *start, *end; const unsigned char *theirs, *start, *end;
const char **ours; const char **ours;
@ -722,6 +722,7 @@ static int ssl_parse_alpn_ext( ssl_context *ssl,
end = buf + len; end = buf + len;
for( ours = ssl->alpn_list; *ours != NULL; ours++ ) for( ours = ssl->alpn_list; *ours != NULL; ours++ )
{ {
ours_len = strlen( *ours );
for( theirs = start; theirs != end; theirs += cur_len ) for( theirs = start; theirs != end; theirs += cur_len )
{ {
/* If the list is well formed, we should get equality first */ /* If the list is well formed, we should get equality first */
@ -734,7 +735,7 @@ static int ssl_parse_alpn_ext( ssl_context *ssl,
if( cur_len == 0 ) if( cur_len == 0 )
return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
if( cur_len == strlen( *ours ) && if( cur_len == ours_len &&
memcmp( theirs, *ours, cur_len ) == 0 ) memcmp( theirs, *ours, cur_len ) == 0 )
{ {
ssl->alpn_chosen = *ours; ssl->alpn_chosen = *ours;

View File

@ -1550,12 +1550,12 @@ static int x509_name_cmp( const void *s1, const void *s2, size_t len )
static int x509_wildcard_verify( const char *cn, x509_buf *name ) static int x509_wildcard_verify( const char *cn, x509_buf *name )
{ {
size_t i; size_t i;
size_t cn_idx = 0; size_t cn_idx = 0, cn_len = strlen( cn );
if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' ) if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
return( 0 ); return( 0 );
for( i = 0; i < strlen( cn ); ++i ) for( i = 0; i < cn_len; ++i )
{ {
if( cn[i] == '.' ) if( cn[i] == '.' )
{ {
@ -1567,7 +1567,7 @@ static int x509_wildcard_verify( const char *cn, x509_buf *name )
if( cn_idx == 0 ) if( cn_idx == 0 )
return( 0 ); return( 0 );
if( strlen( cn ) - cn_idx == name->len - 1 && if( cn_len - cn_idx == name->len - 1 &&
x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 ) x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
{ {
return( 1 ); return( 1 );