[PHP-CVS] [php-src] master: sapi: convert current_user field to zend_string* (#22902)

[email protected] (Gina Peter Banyard via GitHub) Thu, 30 Jul 2026 12:30:13 +0000
Newsgroups php.cvs
Message-ID <[email protected]>
Author: Gina Peter Banyard (Girgias)
Committer: GitHub (web-flow)
Pusher: Girgias
Date: 2026-07-30T13:30:10+01:00

Commit: https://github.com/php/php-src/commit/5795e330a6ac521bfa2e0e9486e9516369860208
Raw diff: https://github.com/php/php-src/commit/5795e330a6ac521bfa2e0e9486e9516369860208.diff

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

Prevents some reallocations and strlen() recomputations

Changed paths:
  M  Zend/Optimizer/zend_func_infos.h
  M  ext/soap/php_sdl.c
  M  ext/standard/basic_functions.c
  M  ext/standard/basic_functions.stub.php
  M  ext/standard/basic_functions_arginfo.h
  M  ext/standard/basic_functions_decl.h
  M  main/SAPI.c
  M  main/SAPI.h
  M  main/main.c
  M  main/php.h


Diff:

diff --git a/Zend/Optimizer/zend_func_infos.h b/Zend/Optimizer/zend_func_infos.h
index 2a9353c9b86e..88d012149beb 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 77d73a2326d7..c6e53c408aa8 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 f8fcc01bf5bb..a87ab1b5ad62 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 7fc31e7d80ca..07e61957d191 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 c5266c5a877c..d24de1fef99d 100644
--- a/ext/standard/basic_functions_arginfo.h
+++ b/ext/standard/basic_functions_arginfo.h
@@ -1,5 +1,5 @@
 /* This is a generated file, edit basic_functions.stub.php instead.
- * Stub hash: 3b1649a3abb3cfb5cb39d93f30a97765fe862d67
+ * Stub hash: 0c8af3b78b3d6e1ee3e59c60363d0d941bea9faa
  * Has decl header: yes */
 
 ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_set_time_limit, 0, 1, _IS_BOOL, 0)
diff --git a/ext/standard/basic_functions_decl.h b/ext/standard/basic_functions_decl.h
index 630a4b7e656b..97d896e8bb6e 100644
--- a/ext/standard/basic_functions_decl.h
+++ b/ext/standard/basic_functions_decl.h
@@ -1,8 +1,8 @@
 /* This is a generated file, edit basic_functions.stub.php instead.
- * Stub hash: 3b1649a3abb3cfb5cb39d93f30a97765fe862d67 */
+ * Stub hash: 0c8af3b78b3d6e1ee3e59c60363d0d941bea9faa */
 
-#ifndef ZEND_BASIC_FUNCTIONS_DECL_3b1649a3abb3cfb5cb39d93f30a97765fe862d67_H
-#define ZEND_BASIC_FUNCTIONS_DECL_3b1649a3abb3cfb5cb39d93f30a97765fe862d67_H
+#ifndef ZEND_BASIC_FUNCTIONS_DECL_0c8af3b78b3d6e1ee3e59c60363d0d941bea9faa_H
+#define ZEND_BASIC_FUNCTIONS_DECL_0c8af3b78b3d6e1ee3e59c60363d0d941bea9faa_H
 
 typedef enum zend_enum_SortDirection {
 	ZEND_ENUM_SortDirection_Ascending = 1,
@@ -20,4 +20,4 @@ typedef enum zend_enum_RoundingMode {
 	ZEND_ENUM_RoundingMode_PositiveInfinity = 8,
 } zend_enum_RoundingMode;
 
-#endif /* ZEND_BASIC_FUNCTIONS_DECL_3b1649a3abb3cfb5cb39d93f30a97765fe862d67_H */
+#endif /* ZEND_BASIC_FUNCTIONS_DECL_0c8af3b78b3d6e1ee3e59c60363d0d941bea9faa_H */
diff --git a/main/SAPI.c b/main/SAPI.c
index 31cf5da18a1d..3daa88e07f25 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 29532a019335..2621b9184d1b 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 4fa34258b983..d0ffe1895e9c 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 fb81a4b47455..5186868e612f 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);