From b95ee002441683e7ed64f173fe21d7e2231b9303 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Thu, 6 Oct 2022 19:11:04 +0100 Subject: [PATCH] Refactor macro-spanning ifs in ecp.c Signed-off-by: David Horstmann --- library/ecp.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/library/ecp.c b/library/ecp.c index dc7363817f..48c2403d42 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -2395,12 +2395,14 @@ cleanup: mbedtls_free( T ); } - /* don't free R while in progress in case R == P */ -#if defined(MBEDTLS_ECP_RESTARTABLE) - if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) -#endif + int should_free_R = 0; /* prevent caller from using invalid value */ - if( ret != 0 ) + should_free_R = ( ret != 0 ); +#if defined(MBEDTLS_ECP_RESTARTABLE) + /* don't free R while in progress in case R == P */ + should_free_R = should_free_R && ( ret != MBEDTLS_ERR_ECP_IN_PROGRESS ); +#endif + if( should_free_R ) mbedtls_ecp_point_free( R ); ECP_RS_LEAVE( rsm ); @@ -2672,10 +2674,12 @@ int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) ); #endif /* MBEDTLS_ECP_INTERNAL_ALT */ + int restarting = 0; #if defined(MBEDTLS_ECP_RESTARTABLE) - /* skip argument check when restarting */ - if( rs_ctx == NULL || rs_ctx->rsm == NULL ) + restarting = ( rs_ctx != NULL && rs_ctx->rsm != NULL ); #endif + /* skip argument check when restarting */ + if( !restarting ) { /* check_privkey is free */ MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_CHK );