Skip to content

Commit 3a2edf1

Browse files
committed
otp_crypto: initialize PSA cleanup sizes before error paths
Declare the PSA output buffer size variables before any goto-based cleanup path can skip their initialization. This fixes Clang -Wsometimes-uninitialized failures in crypto_one_time/4-5 and crypto_update/2 when cleanup frees scratch buffers after early exits.
1 parent cfb44b8 commit 3a2edf1

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

src/libAtomVM/otp_crypto.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,7 @@ static term nif_crypto_crypto_one_time(Context *ctx, int argc, term argv[])
692692
bool encrypt = true;
693693
bool padding_pkcs7 = false;
694694
psa_key_id_t key_id = 0;
695+
size_t output_size = 0;
695696
void *temp_buf = NULL;
696697
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
697698

@@ -751,7 +752,7 @@ static term nif_crypto_crypto_one_time(Context *ctx, int argc, term argv[])
751752
goto psa_error;
752753
}
753754

754-
size_t output_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, data_size);
755+
output_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, data_size);
755756
if (!encrypt) {
756757
output_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, data_size);
757758
}
@@ -2975,12 +2976,13 @@ static term nif_crypto_crypto_update(Context *ctx, int argc, term argv[])
29752976

29762977
void *maybe_allocated_data = NULL;
29772978
void *out_buf = NULL;
2979+
size_t data_len = 0;
2980+
size_t out_size = 0;
29782981

29792982
// from this point onward use `goto cleanup` in order to raise and free all buffers
29802983

29812984
/* 2. Handle iodata input */
29822985
const void *data;
2983-
size_t data_len;
29842986
term iodata_result = handle_iodata(argv[1], &data, &data_len, &maybe_allocated_data);
29852987
if (UNLIKELY(iodata_result == BADARG_ATOM)) {
29862988
SMP_MUTEX_UNLOCK(cipher_state->mutex);
@@ -2994,7 +2996,7 @@ static term nif_crypto_crypto_update(Context *ctx, int argc, term argv[])
29942996
}
29952997

29962998
/* 3. Encrypt/decrypt via PSA - PSA handles internal block buffering */
2997-
size_t out_size = PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(data_len);
2999+
out_size = PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(data_len);
29983000
if (out_size == 0) {
29993001
out_size = 1; /* ensure valid malloc even for zero-length input */
30003002
}

0 commit comments

Comments
 (0)