Add safety check to sample mutex implementation

Due to inconsistent freeing strategy in pkparse.c the sample mutex
implementation in threading.c could lead to undefined behaviour by
destroying the same mutex several times.

This fix prevents mutexes from being destroyed several times in the
sample threading implementation.
This commit is contained in:
Janos Follath 2016-09-26 09:15:44 +01:00
parent 7abc974ec4
commit 5437a75b15
2 changed files with 5 additions and 2 deletions

View File

@ -29,6 +29,8 @@ Bugfix
a contribution from Tobias Tangemann. #541 a contribution from Tobias Tangemann. #541
* Fixed cert_app sample program for debug output and for use when no root * Fixed cert_app sample program for debug output and for use when no root
certificates are provided. certificates are provided.
* Fixed default threading implementation to avoid accidental double
initialisations and double frees.
Changes Changes
* Extended test coverage of special cases, and added new timing test suite. * Extended test coverage of special cases, and added new timing test suite.

View File

@ -32,7 +32,7 @@
#if defined(MBEDTLS_THREADING_PTHREAD) #if defined(MBEDTLS_THREADING_PTHREAD)
static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex ) static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
{ {
if( mutex == NULL ) if( mutex == NULL || mutex->is_valid )
return; return;
mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0; mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
@ -40,10 +40,11 @@ static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex ) static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex )
{ {
if( mutex == NULL ) if( mutex == NULL || !mutex->is_valid )
return; return;
(void) pthread_mutex_destroy( &mutex->mutex ); (void) pthread_mutex_destroy( &mutex->mutex );
mutex->is_valid = 0;
} }
static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex ) static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex )