Commit 1b317bdcc29 for php
commit 1b317bdcc29588ff26485b0457df81c91cdffd5a
Author: Calvin Buckley <calvinb@php.net>
Date: Wed Dec 2 17:46:56 2026 -0400
sapi: Follow-up for post-merge comments on GH-23386 (#26006)
* sapi: use passed through length for input filters
Input filters that didn't change the length would result in an empty
string.
* sapi/litespeed: use zend_string* for doc_root
Unbreak that part.
* sapi/fpm: revert behaviour change for missing env vars
* sapi/litespeed: free doc_root after we're done with it
Note that even before 25c9b8421a632d7e709d17b1245ce9d4a8fb5816,
lsphp leaked:
```
[Tue Dec 1 16:19:57 2026] Script: '-'
/Users/calvin/src/php-src/main/SAPI.c(294) : Freeing 0x0000000108c01200 (25 bytes), script=-
[Tue Dec 1 16:19:57 2026] Script: '-'
/Users/calvin/src/php-src/Zend/zend_ini.c(382) : Freeing 0x0000000108c02000 (56 bytes), script=-
[Tue Dec 1 16:19:57 2026] Script: '-'
/Users/calvin/src/php-src/Zend/zend_hash.c(191) : Freeing 0x0000000108c03000 (320 bytes), script=-
[Tue Dec 1 16:19:57 2026] Script: '-'
/Users/calvin/src/php-src/Zend/zend_string.h(449) : Freeing 0x0000000108c07000 (39 bytes), script=-
[Tue Dec 1 16:19:57 2026] Script: '-'
/Users/calvin/src/php-src/Zend/zend_llist.c(35) : Freeing 0x0000000108c07050 (39 bytes), script=-
=== Total 5 memory leaks detected ===
```
* sapi/litespeed: zero-init ctx for later null check
* sapi: suggested input filter buffer optimization from Nora
* sapi: simplify getenv comment
diff --git a/main/SAPI.c b/main/SAPI.c
index ea3d5c2e392..e03473d2557 100644
--- a/main/SAPI.c
+++ b/main/SAPI.c
@@ -982,8 +982,9 @@ SAPI_API zend_string *sapi_getenv(const char *name, size_t name_len)
}
if (sapi_module.input_filter) {
/* XXX: pretty ugly because input filters aren't zend_string yet. */
- char *tmp = estrdup(ZSTR_VAL(value));
- size_t tmp_len = 0;
+ size_t tmp_len = ZSTR_LEN(value);
+ /* env strings currently can't have nul bytes, safe to strndup */
+ char *tmp = estrndup(ZSTR_VAL(value), tmp_len);
sapi_module.input_filter(PARSE_STRING, name, &tmp, ZSTR_LEN(value), &tmp_len);
zend_string_release(value);
value = zend_string_init(tmp, tmp_len, 0);
diff --git a/sapi/fpm/fpm/fpm_main.c b/sapi/fpm/fpm/fpm_main.c
index 3ca78c09167..3234a4fc294 100644
--- a/sapi/fpm/fpm/fpm_main.c
+++ b/sapi/fpm/fpm/fpm_main.c
@@ -468,11 +468,10 @@ static zend_string *sapi_cgibin_getenv(const char *name, size_t name_len) /* {{{
if (fpm_is_running) {
fcgi_request *request = (fcgi_request*) SG(server_context);
var = fcgi_getenv(request, name, name_len);
+ return var ? zend_string_init(var, strlen(var), 0) : NULL;
}
- if (!var) {
- /* if fpm has not started yet, use std env */
- var = getenv(name); /* XXX: should we? */
- }
+ /* if fpm has not started yet, use std env */
+ var = getenv(name); /* XXX: should we? */
return var ? zend_string_init(var, strlen(var), 0) : NULL;
}
/* }}} */
diff --git a/sapi/litespeed/lsapi_main.c b/sapi/litespeed/lsapi_main.c
index 5c4acbd29f5..9306376c7f0 100644
--- a/sapi/litespeed/lsapi_main.c
+++ b/sapi/litespeed/lsapi_main.c
@@ -911,7 +911,7 @@ static void walk_down_the_path(char* path_start,
typedef struct {
char *path;
uint32_t path_len;
- char *doc_root;
+ zend_string *doc_root;
user_config_cache_entry *entry;
} _lsapi_activate_user_ini_ctx;
@@ -935,7 +935,7 @@ static int lsapi_activate_user_ini_basic_checks(_lsapi_activate_user_ini_ctx *ct
}
ctx->doc_root = sapi_lsapi_getenv("DOCUMENT_ROOT", 0);
- DEBUG_MESSAGE("doc_root: %s", ctx->doc_root);
+ DEBUG_MESSAGE("doc_root: %s", ZSTR_VAL(ctx->doc_root));
if (*fn_next) {
rc = (*fn_next)(ctx, fn_next + 1);
@@ -1031,25 +1031,26 @@ static int lsapi_activate_user_ini_walk_down_the_path(_lsapi_activate_user_ini_c
void* next)
{
time_t request_time = sapi_get_request_time();
+ char *doc_root = ZSTR_VAL(ctx->doc_root);
uint32_t docroot_len;
int rc = SUCCESS;
fn_activate_user_ini_chain_t *fn_next = next;
if (!ctx->entry->expires || request_time > ctx->entry->expires)
{
- docroot_len = ctx->doc_root && ctx->doc_root[0]
- ? pathlen_without_trailing_slash(ctx->doc_root)
+ docroot_len = doc_root && doc_root[0]
+ ? pathlen_without_trailing_slash(doc_root)
: 0;
int is_outside_of_docroot = !docroot_len ||
ctx->path_len < docroot_len ||
- strncmp(ctx->path, ctx->doc_root, docroot_len) != 0;
+ strncmp(ctx->path, doc_root, docroot_len) != 0;
if (is_outside_of_docroot) {
php_parse_user_ini_file(ctx->path, PG(user_ini_filename),
&ctx->entry->user_config);
} else {
- walk_down_the_path(ctx->doc_root, ctx->path,
+ walk_down_the_path(doc_root, ctx->path,
&walk_down_the_path_callback, ctx);
}
@@ -1081,7 +1082,7 @@ static int lsapi_activate_user_ini_finally(_lsapi_activate_user_ini_ctx *ctx,
static int lsapi_activate_user_ini( void )
{
- _lsapi_activate_user_ini_ctx ctx;
+ _lsapi_activate_user_ini_ctx ctx = {0};
/**
* The reason to have this function list stacked
* is each function now can define a scoped destructor.
@@ -1101,7 +1102,11 @@ static int lsapi_activate_user_ini( void )
NULL
};
- return fn_chain[0](&ctx, (fn_activate_user_ini_chain_t*)(fn_chain + 1));
+ int ret = fn_chain[0](&ctx, (fn_activate_user_ini_chain_t*)(fn_chain + 1));
+ if (ctx.doc_root) {
+ zend_string_release(ctx.doc_root);
+ }
+ return ret;
}