From 1f19fa6f62346d57d5dac41512cc107c8a7fc7fb Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 19 Dec 2018 14:18:39 +0100 Subject: [PATCH] PK: Fix free(NULL) in library and tests free() functions are documented as no-ops on NULL. Implement and test this correctly. --- library/pk.c | 3 +-- tests/suites/test_suite_pk.function | 8 ++++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/library/pk.c b/library/pk.c index d8bce8f46..38ab7747c 100644 --- a/library/pk.c +++ b/library/pk.c @@ -90,8 +90,7 @@ void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx ) */ void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx ) { - PK_VALIDATE( ctx != NULL ); - if( ctx->pk_info == NULL || + if( ctx == NULL || ctx->pk_info == NULL || ctx->pk_info->rs_free_func == NULL ) { return; diff --git a/tests/suites/test_suite_pk.function b/tests/suites/test_suite_pk.function index 936bcdfb2..b8069b540 100644 --- a/tests/suites/test_suite_pk.function +++ b/tests/suites/test_suite_pk.function @@ -77,6 +77,12 @@ void valid_parameters( ) mbedtls_pk_init( &pk ); + TEST_VALID_PARAM( mbedtls_pk_free( NULL ) ); + +#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) + TEST_VALID_PARAM( mbedtls_pk_restart_free( NULL ) ); +#endif + TEST_ASSERT( mbedtls_pk_setup( &pk, NULL ) == MBEDTLS_ERR_PK_BAD_INPUT_DATA ); @@ -133,8 +139,6 @@ void invalid_parameters( ) #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) TEST_INVALID_PARAM( mbedtls_pk_restart_init( NULL ) ); - - TEST_INVALID_PARAM( mbedtls_pk_restart_free( NULL ) ); #endif TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,