Skip to content

Commit 082417b

Browse files
Fix memory failure handling in test_format_storage_data_check
Fail the test instead of crashing if a memory allocation fails. Free memory even if the test fails.
1 parent 28867e4 commit 082417b

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

tests/suites/test_suite_psa_crypto_persistent_key.function

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ void format_storage_data_check( data_t *key_data,
3232
int key_lifetime, int key_type,
3333
int key_usage, int key_alg, int key_alg2 )
3434
{
35-
uint8_t *file_data;
36-
size_t file_data_length;
35+
uint8_t *file_data = NULL;
36+
size_t file_data_length =
37+
key_data->len + sizeof( psa_persistent_key_storage_format );;
3738
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3839

3940
psa_set_key_lifetime( &attributes, key_lifetime );
@@ -42,14 +43,15 @@ void format_storage_data_check( data_t *key_data,
4243
psa_set_key_algorithm( &attributes, key_alg );
4344
psa_set_key_enrollment_algorithm( &attributes, key_alg2 );
4445

45-
file_data_length = key_data->len + sizeof( psa_persistent_key_storage_format );
46-
file_data = mbedtls_calloc( 1, file_data_length );
46+
ASSERT_ALLOC( file_data, file_data_length );
4747
psa_format_key_data_for_storage( key_data->x, key_data->len,
4848
&attributes.core,
4949
file_data );
5050

5151
ASSERT_COMPARE( expected_file_data->x, expected_file_data->len,
5252
file_data, file_data_length );
53+
54+
exit:
5355
mbedtls_free( file_data );
5456
}
5557
/* END_CASE */

0 commit comments

Comments
 (0)