tests: free PKCS7 cert stack when encode_certs fails

wolfSSL_PKCS7_encode_certs takes the stack only on success. These tests
dropped sk whenever p7 and the BIO were non-NULL, which leaked under
MEM_FAIL_CNT.

Signed-off-by: Sameeh Jubran <sameeh@wolfssl.com>
pull/11341/head
Sameeh Jubran 2026-09-01 19:53:37 +03:00
parent b273b7d11b
commit 4bef71c2df
1 changed files with 10 additions and 10 deletions

View File

@ -154,10 +154,14 @@ int test_wolfSSL_PKCS7_certs(void)
bio = NULL;
ExpectNotNull(bio = BIO_new(BIO_s_mem()));
ExpectIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
if ((sk != NULL) && ((p7 == NULL) || (bio == NULL))) {
sk_X509_pop_free(sk, X509_free);
/* encode_certs takes sk only on success. */
if (EXPECT_SUCCESS()) {
sk = NULL;
}
else if (sk != NULL) {
sk_X509_pop_free(sk, X509_free);
sk = NULL;
}
sk = NULL;
ExpectIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
if (i == 0) {
@ -721,14 +725,10 @@ int test_wolfSSL_PKCS7_verify_degenerate(void)
#endif
}
ExpectNotNull(derBio = BIO_new(BIO_s_mem()));
/* wolfSSL_PKCS7_encode_certs() takes ownership of sk (sets p7->certs, freed
* by PKCS7_free(encodeP7) below) whenever it is called with a valid PKCS7
* and BIO - success or failure. It only declines ownership when encodeP7 or
* derBio is NULL, in which case the test still owns sk and frees it in
* cleanup. */
/* encode_certs takes sk only on success; cleanup frees it otherwise. */
ExpectIntEQ(wolfSSL_PKCS7_encode_certs(encodeP7, sk, derBio), 1);
if (encodeP7 != NULL && derBio != NULL)
sk = NULL; /* now owned by encodeP7 */
if (EXPECT_SUCCESS())
sk = NULL;
ExpectIntGT((derSz = BIO_get_mem_data(derBio, &der)), 0);
/* ---- Re-parse it; a degenerate bundle parses successfully. ---- */