From f226998fa25f5a3cd17eb48b542fa36907dc8b37 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Wed, 20 Feb 2019 14:43:55 +0000 Subject: [PATCH] Reduce code-size of mbedtls_asn1_get_sequence_of() Reduce nesting of branches and remove unnecessary check at the end of the routine. --- library/asn1parse.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/library/asn1parse.c b/library/asn1parse.c index b844fc69e..dffc67fb3 100644 --- a/library/asn1parse.c +++ b/library/asn1parse.c @@ -251,7 +251,7 @@ int mbedtls_asn1_get_sequence_of( unsigned char **p, if( *p + len != end ) return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); - while( *p < end ) + while( 1 ) { buf = &(cur->buf); buf->tag = **p; @@ -262,25 +262,20 @@ int mbedtls_asn1_get_sequence_of( unsigned char **p, buf->p = *p; *p += buf->len; - /* Allocate and assign next pointer */ - if( *p < end ) + if( *p == end ) { - cur->next = (mbedtls_asn1_sequence*)mbedtls_calloc( 1, - sizeof( mbedtls_asn1_sequence ) ); - - if( cur->next == NULL ) - return( MBEDTLS_ERR_ASN1_ALLOC_FAILED ); - - cur = cur->next; + cur->next = NULL; + break; } + + cur->next = (mbedtls_asn1_sequence*)mbedtls_calloc( 1, + sizeof( mbedtls_asn1_sequence ) ); + if( cur->next == NULL ) + return( MBEDTLS_ERR_ASN1_ALLOC_FAILED ); + + cur = cur->next; } - /* Set final sequence entry's next pointer to NULL */ - cur->next = NULL; - - if( *p != end ) - return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); - return( 0 ); }