[php-src] PHP-8.5: Simplify php_openssl_load_all_certs_from_file()
| From: | Theo Buehler via Jakub Zelenka | Date: | Tue, 24 Nov 2026 12:09:58 +0000 |
| Subject: | [php-src] PHP-8.5: Simplify php_openssl_load_all_certs_from_file() | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-144100@lists.php.net to get a copy of this message | ||
Author: Theo Buehler (botovq)
Committer: Jakub Zelenka (bukka)
Date: 2026-11-24T12:39:06+01:00
Commit: https://github.com/php/php-src/commit/227a771617d1aa37eca8c63801aa9b8009bfbdd3
Raw diff: https://github.com/php/php-src/commit/227a771617d1aa37eca8c63801aa9b8009bfbdd3.diff
Simplify php_openssl_load_all_certs_from_file()
Correctly free both stacks, sk and stack, in the exit path and transfer
ownership of stack to ret on success. Don't free the members of sk
in the loop, only steal the certs after successful sk_X509_push(), which
is error checked as it always should be. Switch from sk_new_reserve()
back to the much saner sk_X509_new_null(). This makes ownership in this
code easier to reason about and more robust to future modifications.
Closes GH-21430
Changed paths:
M ext/openssl/openssl_backend_common.c
Diff:
diff --git a/ext/openssl/openssl_backend_common.c b/ext/openssl/openssl_backend_common.c
index a5bd7238d9ed..21ccf2138edc 100644
--- a/ext/openssl/openssl_backend_common.c
+++ b/ext/openssl/openssl_backend_common.c
@@ -695,6 +695,7 @@ STACK_OF(X509) *php_openssl_load_all_certs_from_file(
BIO *in=NULL;
X509_INFO *xi;
char cert_path[MAXPATHLEN];
+ int i;
if (!php_openssl_check_path(cert_file, cert_file_len, cert_path, arg_num)) {
goto end;
@@ -713,29 +714,32 @@ STACK_OF(X509) *php_openssl_load_all_certs_from_file(
goto end;
}
- if(!(stack = sk_X509_new_reserve(NULL, sk_X509_INFO_num(sk)))) {
+ if(!(stack = sk_X509_new_null())) {
php_openssl_store_errors();
goto end;
}
/* scan over it and pull out the certs */
- while (sk_X509_INFO_num(sk)) {
- xi=sk_X509_INFO_shift(sk);
+ for (i = 0; i < sk_X509_INFO_num(sk); i++) {
+ xi=sk_X509_INFO_value(sk,i);
if (xi->x509 != NULL) {
- sk_X509_push(stack,xi->x509);
+ if (!sk_X509_push(stack,xi->x509)) {
+ php_error_docref(NULL, E_ERROR, "Memory allocation failure");
+ goto end;
+ }
xi->x509=NULL;
}
- X509_INFO_free(xi);
}
if (!sk_X509_num(stack)) {
php_error_docref(NULL, E_WARNING, "No certificates in file, %s", cert_path);
- sk_X509_free(stack);
goto end;
}
ret = stack;
+ stack = NULL;
end:
BIO_free(in);
- sk_X509_INFO_free(sk);
+ sk_X509_INFO_pop_free(sk,X509_INFO_free);
+ sk_X509_pop_free(stack,X509_free);
return ret;
}