Skip to content

Commit 8f111cd

Browse files
committed
sapi: convert current_user field to zend_string*
Prevents some reallocations and strlen() recomputations
1 parent 3a6ef63 commit 8f111cd

10 files changed

Lines changed: 22 additions & 33 deletions

File tree

Zend/Optimizer/zend_func_infos.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,6 @@ static const func_info_t func_infos[] = {
419419
#if defined(HAVE_NANOSLEEP)
420420
F1("time_nanosleep", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_BOOL),
421421
#endif
422-
F1("get_current_user", MAY_BE_STRING),
423422
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),
424423
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),
425424
F1("highlight_file", MAY_BE_STRING|MAY_BE_BOOL),

ext/soap/php_sdl.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3179,8 +3179,7 @@ sdlPtr get_sdl(zval *this_ptr, char *uri, zend_long cache_wsdl)
31793179
unsigned char digest[16];
31803180
size_t len = strlen(SOAP_GLOBAL(cache_dir));
31813181
time_t cached;
3182-
char *user = php_get_current_user();
3183-
size_t user_len = user ? strlen(user) + 1 : 0;
3182+
zend_string *user = php_get_current_user();
31843183

31853184
/* System architecture identification (see bug #70951) */
31863185
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)
31913190
PHP_MD5Update(&md5_context, ids, sizeof(ids));
31923191
PHP_MD5Final(digest, &md5_context);
31933192
make_digest(md5str, digest);
3194-
key = emalloc(len+sizeof("/wsdl-")-1+user_len+2+sizeof(md5str));
3193+
key = emalloc(len+sizeof("/wsdl-")-1+ZSTR_LEN(user)+2+sizeof(md5str));
31953194
memcpy(key,SOAP_GLOBAL(cache_dir),len);
31963195
memcpy(key+len,"/wsdl-",sizeof("/wsdl-")-1);
31973196
len += sizeof("/wsdl-")-1;
3198-
if (user_len) {
3199-
memcpy(key+len, user, user_len-1);
3200-
len += user_len-1;
3197+
if (ZSTR_LEN(user)) {
3198+
memcpy(key+len, ZSTR_VAL(user), ZSTR_LEN(user)-1);
3199+
len += ZSTR_LEN(user)-1;
32013200
key[len++] = '-';
32023201
}
32033202
if (WSDL_CACHE_VERSION <= 0x9f) {

ext/standard/basic_functions.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ PHP_FUNCTION(get_current_user)
12551255
{
12561256
ZEND_PARSE_PARAMETERS_NONE();
12571257

1258-
RETURN_STRING(php_get_current_user());
1258+
RETURN_STR_COPY(php_get_current_user());
12591259
}
12601260
/* }}} */
12611261

ext/standard/basic_functions.stub.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1976,7 +1976,6 @@ function time_nanosleep(int $seconds, int $nanoseconds): array|bool {}
19761976
function time_sleep_until(float $timestamp): bool {}
19771977
#endif
19781978

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

19821981
/** @return string|array<int|string, string|array>|false */

ext/standard/basic_functions_arginfo.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/standard/basic_functions_decl.h

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main/SAPI.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,6 @@ SAPI_API void sapi_activate_headers_only(void)
370370
SG(read_post_bytes) = 0;
371371
SG(request_info).request_body = NULL;
372372
SG(request_info).current_user = NULL;
373-
SG(request_info).current_user_length = 0;
374373
SG(request_info).no_headers = 0;
375374
SG(request_info).post_entry = NULL;
376375
SG(global_request_time) = 0;
@@ -413,7 +412,6 @@ SAPI_API void sapi_activate(void)
413412
SG(read_post_bytes) = 0;
414413
SG(request_info).request_body = NULL;
415414
SG(request_info).current_user = NULL;
416-
SG(request_info).current_user_length = 0;
417415
SG(request_info).no_headers = 0;
418416
SG(request_info).post_entry = NULL;
419417
SG(request_info).proto_num = 1000; /* Default to HTTP 1.0 */
@@ -499,7 +497,7 @@ SAPI_API void sapi_deactivate_module(void)
499497
efree(SG(request_info).content_type_dup);
500498
}
501499
if (SG(request_info).current_user) {
502-
efree(SG(request_info).current_user);
500+
zend_string_release_ex(SG(request_info).current_user, false);
503501
}
504502
if (sapi_module.deactivate) {
505503
sapi_module.deactivate();

main/SAPI.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ typedef struct {
9797
/* this is necessary for the CGI SAPI module */
9898
char *argv0;
9999

100-
char *current_user;
101-
int current_user_length;
100+
zend_string *current_user;
102101

103102
/* this is necessary for CLI module */
104103
int argc;

main/main.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,7 +1508,7 @@ static ZEND_COLD void php_error_cb(int orig_type, zend_string *error_filename, c
15081508
/* }}} */
15091509

15101510
/* {{{ php_get_current_user */
1511-
PHPAPI char *php_get_current_user(void)
1511+
PHPAPI zend_string *php_get_current_user(void)
15121512
{
15131513
zend_stat_t *pstat = NULL;
15141514

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

15251525
if (!pstat) {
1526-
return "";
1526+
return ZSTR_EMPTY_ALLOC();
15271527
} else {
15281528
#ifdef PHP_WIN32
15291529
char *name = php_win32_get_username();
1530-
int len;
15311530

15321531
if (!name) {
1533-
return "";
1532+
return ZSTR_EMPTY_ALLOC();
15341533
}
1535-
len = (int)strlen(name);
1536-
name[len] = '\0';
1537-
SG(request_info).current_user_length = len;
1538-
SG(request_info).current_user = estrndup(name, len);
1534+
SG(request_info).current_user = zend_string_init(name, strlen(name), false);
15391535
free(name);
15401536
return SG(request_info).current_user;
15411537
#else
@@ -1565,20 +1561,19 @@ PHPAPI char *php_get_current_user(void)
15651561
goto try_again;
15661562
}
15671563
efree(pwbuf);
1568-
return "";
1564+
return ZSTR_EMPTY_ALLOC();
15691565
}
15701566
if (retpwptr == NULL) {
15711567
efree(pwbuf);
1572-
return "";
1568+
return ZSTR_EMPTY_ALLOC();
15731569
}
15741570
pwd = &_pw;
15751571
#else
15761572
if ((pwd=getpwuid(pstat->st_uid))==NULL) {
1577-
return "";
1573+
return ZSTR_EMPTY_ALLOC();
15781574
}
15791575
#endif
1580-
SG(request_info).current_user_length = strlen(pwd->pw_name);
1581-
SG(request_info).current_user = estrndup(pwd->pw_name, SG(request_info).current_user_length);
1576+
SG(request_info).current_user = zend_string_init(pwd->pw_name, strlen(pwd->pw_name), false);
15821577
#if defined(ZTS) && defined(HAVE_GETPWUID_R) && defined(_SC_GETPW_R_SIZE_MAX)
15831578
efree(pwbuf);
15841579
#endif

main/php.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ PHPAPI extern int (*php_register_internal_extensions_func)(void);
323323
PHPAPI int php_register_internal_extensions(void);
324324
PHPAPI void php_register_pre_request_shutdown(void (*func)(void *), void *userdata);
325325
PHPAPI void php_com_initialize(void);
326-
PHPAPI char *php_get_current_user(void);
326+
PHPAPI zend_string *php_get_current_user(void);
327327

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

0 commit comments

Comments
 (0)