Commit 5795e330a6a for php.net

commit 5795e330a6ac521bfa2e0e9486e9516369860208
Author: Gina Peter Banyard <girgias@php.net>
Date:   Thu Jul 30 13:30:10 2026 +0100

    sapi: convert current_user field to zend_string* (#22902)

    Prevents some reallocations and strlen() recomputations

diff --git a/Zend/Optimizer/zend_func_infos.h b/Zend/Optimizer/zend_func_infos.h
index 2a9353c9b86..88d012149be 100644
--- a/Zend/Optimizer/zend_func_infos.h
+++ b/Zend/Optimizer/zend_func_infos.h
@@ -419,7 +419,6 @@ static const func_info_t func_infos[] = {
 #if defined(HAVE_NANOSLEEP)
 	F1("time_nanosleep", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_BOOL),
 #endif
-	F1("get_current_user", MAY_BE_STRING),
 	FN("get_cfg_var", MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_ARRAY|MAY_BE_FALSE),
 	F1("error_get_last", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_ARRAY|MAY_BE_NULL),
 	F1("highlight_file", MAY_BE_STRING|MAY_BE_BOOL),
diff --git a/ext/soap/php_sdl.c b/ext/soap/php_sdl.c
index 77d73a2326d..c6e53c408aa 100644
--- a/ext/soap/php_sdl.c
+++ b/ext/soap/php_sdl.c
@@ -3179,8 +3179,7 @@ sdlPtr get_sdl(zval *this_ptr, char *uri, zend_long cache_wsdl)
 		unsigned char digest[16];
 		size_t len = strlen(SOAP_GLOBAL(cache_dir));
 		time_t cached;
-		char *user = php_get_current_user();
-		size_t user_len = user ? strlen(user) + 1 : 0;
+		zend_string *user = php_get_current_user();

 		/* System architecture identification (see bug #70951) */
 		static const char ids[] = {SIZEOF_ZEND_LONG, SOAP_BIG_ENDIAN};
@@ -3191,13 +3190,13 @@ sdlPtr get_sdl(zval *this_ptr, char *uri, zend_long cache_wsdl)
 		PHP_MD5Update(&md5_context, ids, sizeof(ids));
 		PHP_MD5Final(digest, &md5_context);
 		make_digest(md5str, digest);
-		key = emalloc(len+sizeof("/wsdl-")-1+user_len+2+sizeof(md5str));
+		key = emalloc(len+sizeof("/wsdl-")-1+ZSTR_LEN(user)+1+2+sizeof(md5str));
 		memcpy(key,SOAP_GLOBAL(cache_dir),len);
 		memcpy(key+len,"/wsdl-",sizeof("/wsdl-")-1);
 		len += sizeof("/wsdl-")-1;
-		if (user_len) {
-			memcpy(key+len, user, user_len-1);
-			len += user_len-1;
+		if (ZSTR_LEN(user)) {
+			memcpy(key+len, ZSTR_VAL(user), ZSTR_LEN(user));
+			len += ZSTR_LEN(user);
 			key[len++] = '-';
 		}
 		if (WSDL_CACHE_VERSION <= 0x9f) {
diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c
index f8fcc01bf5b..a87ab1b5ad6 100644
--- a/ext/standard/basic_functions.c
+++ b/ext/standard/basic_functions.c
@@ -1255,7 +1255,7 @@ PHP_FUNCTION(get_current_user)
 {
 	ZEND_PARSE_PARAMETERS_NONE();

-	RETURN_STRING(php_get_current_user());
+	RETURN_STR_COPY(php_get_current_user());
 }
 /* }}} */

diff --git a/ext/standard/basic_functions.stub.php b/ext/standard/basic_functions.stub.php
index 7fc31e7d80c..07e61957d19 100644
--- a/ext/standard/basic_functions.stub.php
+++ b/ext/standard/basic_functions.stub.php
@@ -1976,7 +1976,6 @@ function time_nanosleep(int $seconds, int $nanoseconds): array|bool {}
 function time_sleep_until(float $timestamp): bool {}
 #endif

-/** @refcount 1 */
 function get_current_user(): string {}

 /** @return string|array<int|string, string|array>|false */
diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h
index c5266c5a877..d24de1fef99 100644
Binary files a/ext/standard/basic_functions_arginfo.h and b/ext/standard/basic_functions_arginfo.h differ
diff --git a/ext/standard/basic_functions_decl.h b/ext/standard/basic_functions_decl.h
index 630a4b7e656..97d896e8bb6 100644
Binary files a/ext/standard/basic_functions_decl.h and b/ext/standard/basic_functions_decl.h differ
diff --git a/main/SAPI.c b/main/SAPI.c
index 31cf5da18a1..3daa88e07f2 100644
--- a/main/SAPI.c
+++ b/main/SAPI.c
@@ -370,7 +370,6 @@ SAPI_API void sapi_activate_headers_only(void)
 	SG(read_post_bytes) = 0;
 	SG(request_info).request_body = NULL;
 	SG(request_info).current_user = NULL;
-	SG(request_info).current_user_length = 0;
 	SG(request_info).no_headers = 0;
 	SG(request_info).post_entry = NULL;
 	SG(global_request_time) = 0;
@@ -413,7 +412,6 @@ SAPI_API void sapi_activate(void)
 	SG(read_post_bytes) = 0;
 	SG(request_info).request_body = NULL;
 	SG(request_info).current_user = NULL;
-	SG(request_info).current_user_length = 0;
 	SG(request_info).no_headers = 0;
 	SG(request_info).post_entry = NULL;
 	SG(request_info).proto_num = 1000; /* Default to HTTP 1.0 */
@@ -499,7 +497,7 @@ SAPI_API void sapi_deactivate_module(void)
 		efree(SG(request_info).content_type_dup);
 	}
 	if (SG(request_info).current_user) {
-		efree(SG(request_info).current_user);
+		zend_string_release_ex(SG(request_info).current_user, false);
 	}
 	if (sapi_module.deactivate) {
 		sapi_module.deactivate();
diff --git a/main/SAPI.h b/main/SAPI.h
index 29532a01933..2621b9184d1 100644
--- a/main/SAPI.h
+++ b/main/SAPI.h
@@ -97,8 +97,7 @@ typedef struct {
 	/* this is necessary for the CGI SAPI module */
 	char *argv0;

-	char *current_user;
-	int current_user_length;
+	zend_string *current_user;

 	/* this is necessary for CLI module */
 	int argc;
diff --git a/main/main.c b/main/main.c
index 4fa34258b98..d0ffe1895e9 100644
--- a/main/main.c
+++ b/main/main.c
@@ -1509,7 +1509,7 @@ static ZEND_COLD void php_error_cb(int orig_type, zend_string *error_filename, c
 /* }}} */

 /* {{{ php_get_current_user */
-PHPAPI char *php_get_current_user(void)
+PHPAPI zend_string *php_get_current_user(void)
 {
 	zend_stat_t *pstat = NULL;

@@ -1524,19 +1524,15 @@ PHPAPI char *php_get_current_user(void)
 	pstat = sapi_get_stat();

 	if (!pstat) {
-		return "";
+		return ZSTR_EMPTY_ALLOC();
 	} else {
 #ifdef PHP_WIN32
 		char *name = php_win32_get_username();
-		int len;

 		if (!name) {
-			return "";
+			return ZSTR_EMPTY_ALLOC();
 		}
-		len = (int)strlen(name);
-		name[len] = '\0';
-		SG(request_info).current_user_length = len;
-		SG(request_info).current_user = estrndup(name, len);
+		SG(request_info).current_user = zend_string_init(name, strlen(name), false);
 		free(name);
 		return SG(request_info).current_user;
 #else
@@ -1566,20 +1562,19 @@ PHPAPI char *php_get_current_user(void)
 				goto try_again;
 			}
 			efree(pwbuf);
-			return "";
+			return ZSTR_EMPTY_ALLOC();
 		}
 		if (retpwptr == NULL) {
 			efree(pwbuf);
-			return "";
+			return ZSTR_EMPTY_ALLOC();
 		}
 		pwd = &_pw;
 #else
 		if ((pwd=getpwuid(pstat->st_uid))==NULL) {
-			return "";
+			return ZSTR_EMPTY_ALLOC();
 		}
 #endif
-		SG(request_info).current_user_length = strlen(pwd->pw_name);
-		SG(request_info).current_user = estrndup(pwd->pw_name, SG(request_info).current_user_length);
+		SG(request_info).current_user = zend_string_init(pwd->pw_name, strlen(pwd->pw_name), false);
 #if defined(ZTS) && defined(HAVE_GETPWUID_R) && defined(_SC_GETPW_R_SIZE_MAX)
 		efree(pwbuf);
 #endif
diff --git a/main/php.h b/main/php.h
index fb81a4b4745..5186868e612 100644
--- a/main/php.h
+++ b/main/php.h
@@ -323,7 +323,7 @@ PHPAPI extern int (*php_register_internal_extensions_func)(void);
 PHPAPI int php_register_internal_extensions(void);
 PHPAPI void php_register_pre_request_shutdown(void (*func)(void *), void *userdata);
 PHPAPI void php_com_initialize(void);
-PHPAPI char *php_get_current_user(void);
+PHPAPI zend_string *php_get_current_user(void);

 PHPAPI const char *php_get_internal_encoding(void);
 PHPAPI const char *php_get_input_encoding(void);