Fix an infinite loop if cleanup fails in some tests

Don't call a macro that does `goto exit` on failure after the `exit:` label:
that would cause an infinite loop if something does go wrong.

Generally, cleanup functions don't error out, so it is unlikely to be a
problem in practice. If an error does happen during cleanup, it's probably
due to memory corruption caused by a bug that happened earlier, and that is
likely to have been detected in an earlier function. So we don't really need
to assert the return code of functions called during cleanup, and normally
we don't. Only a few places did so, wrongly.

I found the problematic places with
```
ag 'exit:[^}]*(PSA_ASSERT|TEST_ASSERT|TEST_EQUAL)' tests/suites/*.function
```

Signed-off-by: Gilles Peskine <[email protected]>
This commit is contained in:
Gilles Peskine
2026-04-10 11:48:46 +02:00
parent dd921414d3
commit 4877c0838d
4 changed files with 19 additions and 17 deletions
+1 -1
View File
@@ -781,7 +781,7 @@ void pk_can_do_ext(int opaque_key, int key_type, int key_usage, int key_alg,
exit:
psa_reset_key_attributes(&attributes);
PSA_ASSERT(psa_destroy_key(key));
psa_destroy_key(key);
mbedtls_pk_free(&pk);
USE_PSA_DONE();
}
+7 -7
View File
@@ -2411,7 +2411,7 @@ void aead_key_policy(int policy_usage_arg,
}
exit:
PSA_ASSERT(psa_aead_abort(&operation));
psa_aead_abort(&operation);
psa_destroy_key(key);
PSA_DONE();
}
@@ -3030,7 +3030,7 @@ void hash_compute_fail(int alg_arg, data_t *input,
}
exit:
PSA_ASSERT(psa_hash_abort(&operation));
psa_hash_abort(&operation);
mbedtls_free(output);
PSA_DONE();
}
@@ -3069,7 +3069,7 @@ void hash_compare_fail(int alg_arg, data_t *input,
}
exit:
PSA_ASSERT(psa_hash_abort(&operation));
psa_hash_abort(&operation);
PSA_DONE();
}
/* END_CASE */
@@ -3173,7 +3173,7 @@ void hash_compute_compare(int alg_arg, data_t *input,
}
exit:
PSA_ASSERT(psa_hash_abort(&operation));
psa_hash_abort(&operation);
PSA_DONE();
}
/* END_CASE */
@@ -4345,7 +4345,7 @@ void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
output, output_length);
exit:
PSA_ASSERT(psa_cipher_abort(&operation));
psa_cipher_abort(&operation);
mbedtls_free(output);
psa_cipher_abort(&operation);
psa_destroy_key(key);
@@ -10967,8 +10967,8 @@ void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
}
exit:
PSA_ASSERT(psa_destroy_key(key));
PSA_ASSERT(psa_pake_abort(&operation));
psa_destroy_key(key);
psa_pake_abort(&operation);
mbedtls_free(output_buffer);
PSA_DONE();
}
@@ -2899,7 +2899,7 @@ void aead_encrypt_setup(int key_type_arg, data_t *key_data,
exit:
/* Cleanup */
PSA_ASSERT(psa_destroy_key(key));
psa_destroy_key(key);
mbedtls_free(output_data);
PSA_DONE();
mbedtls_test_driver_aead_hooks = mbedtls_test_driver_aead_hooks_init();
@@ -3001,7 +3001,7 @@ void aead_decrypt_setup(int key_type_arg, data_t *key_data,
}
exit:
PSA_ASSERT(psa_destroy_key(key));
psa_destroy_key(key);
mbedtls_free(output_data);
PSA_DONE();
}
@@ -763,8 +763,8 @@ void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
}
exit:
PSA_ASSERT(psa_destroy_key(key));
PSA_ASSERT(psa_pake_abort(&operation));
psa_destroy_key(key);
psa_pake_abort(&operation);
mbedtls_free(output_buffer);
PSA_DONE();
}
@@ -1035,8 +1035,8 @@ void pake_input_getters_password()
TEST_MEMORY_COMPARE(password_ret, buffer_len_ret, password, strlen(password));
exit:
PSA_ASSERT(psa_destroy_key(key));
PSA_ASSERT(psa_pake_abort(&operation));
psa_destroy_key(key);
psa_pake_abort(&operation);
PSA_DONE();
}
/* END_CASE */
@@ -1071,7 +1071,7 @@ void pake_input_getters_cipher_suite()
PSA_ERROR_BAD_STATE);
exit:
PSA_ASSERT(psa_pake_abort(&operation));
psa_pake_abort(&operation);
PSA_DONE();
}
/* END_CASE */
@@ -1133,8 +1133,9 @@ void pake_input_getters_user()
TEST_MEMORY_COMPARE(user_ret, buffer_len_ret, user, user_len);
}
exit:
PSA_ASSERT(psa_pake_abort(&operation));
psa_pake_abort(&operation);
PSA_DONE();
}
/* END_CASE */
@@ -1196,8 +1197,9 @@ void pake_input_getters_peer()
TEST_MEMORY_COMPARE(peer_ret, buffer_len_ret, peer, peer_len);
}
exit:
PSA_ASSERT(psa_pake_abort(&operation));
psa_pake_abort(&operation);
PSA_DONE();
}
/* END_CASE */