Skip to content

Commit f5166b3

Browse files
committed
ext/session: convert global session_name to zstr
1 parent b83484d commit f5166b3

File tree

2 files changed

+21
-29
lines changed

2 files changed

+21
-29
lines changed

ext/session/php_session.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ typedef struct _php_session_rfc1867_progress {
140140

141141
typedef struct _php_ps_globals {
142142
zend_string *save_path;
143-
char *session_name;
143+
zend_string *session_name;
144144
zend_string *id;
145145
char *extern_referer_chk;
146146
char *cache_limiter;

ext/session/session.c

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ static zend_result php_session_initialize(void)
425425
}
426426

427427
/* Open session handler first */
428-
if (PS(mod)->s_open(&PS(mod_data), ZSTR_VAL(PS(save_path)), PS(session_name)) == FAILURE
428+
if (PS(mod)->s_open(&PS(mod_data), ZSTR_VAL(PS(save_path)), ZSTR_VAL(PS(session_name))) == FAILURE
429429
/* || PS(mod_data) == NULL */ /* FIXME: open must set valid PS(mod_data) with success */
430430
) {
431431
php_session_abort();
@@ -706,7 +706,7 @@ static PHP_INI_MH(OnUpdateName)
706706
return FAILURE;
707707
}
708708

709-
return OnUpdateStringUnempty(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
709+
return OnUpdateStrNotEmpty(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
710710
}
711711

712712

@@ -1349,10 +1349,9 @@ static void php_session_remove_cookie(void) {
13491349
size_t session_cookie_len;
13501350
size_t len = sizeof("Set-Cookie")-1;
13511351

1352-
ZEND_ASSERT(strpbrk(PS(session_name), SESSION_FORBIDDEN_CHARS) == NULL);
1353-
spprintf(&session_cookie, 0, "Set-Cookie: %s=", PS(session_name));
1352+
ZEND_ASSERT(strpbrk(ZSTR_VAL(PS(session_name)), SESSION_FORBIDDEN_CHARS) == NULL);
1353+
session_cookie_len = spprintf(&session_cookie, 0, "Set-Cookie: %s=", ZSTR_VAL(PS(session_name)));
13541354

1355-
session_cookie_len = strlen(session_cookie);
13561355
current = l->head;
13571356
while (current) {
13581357
header = (sapi_header_struct *)(current->data);
@@ -1389,13 +1388,13 @@ static zend_result php_session_send_cookie(void)
13891388
return FAILURE;
13901389
}
13911390

1392-
ZEND_ASSERT(strpbrk(PS(session_name), SESSION_FORBIDDEN_CHARS) == NULL);
1391+
ZEND_ASSERT(strpbrk(ZSTR_VAL(PS(session_name)), SESSION_FORBIDDEN_CHARS) == NULL);
13931392

13941393
/* URL encode id because it might be user supplied */
13951394
e_id = php_url_encode(ZSTR_VAL(PS(id)), ZSTR_LEN(PS(id)));
13961395

13971396
smart_str_appends(&ncookie, "Set-Cookie: ");
1398-
smart_str_appendl(&ncookie, PS(session_name), strlen(PS(session_name)));
1397+
smart_str_append(&ncookie, PS(session_name));
13991398
smart_str_appendc(&ncookie, '=');
14001399
smart_str_append(&ncookie, e_id);
14011400

@@ -1521,7 +1520,7 @@ PHPAPI zend_result php_session_reset_id(void)
15211520
if (PS(define_sid)) {
15221521
smart_str var = {0};
15231522

1524-
smart_str_appends(&var, PS(session_name));
1523+
smart_str_append(&var, PS(session_name));
15251524
smart_str_appendc(&var, '=');
15261525
smart_str_append(&var, PS(id));
15271526
smart_str_0(&var);
@@ -1549,18 +1548,15 @@ PHPAPI zend_result php_session_reset_id(void)
15491548
(data = zend_hash_str_find(&EG(symbol_table), ZEND_STRL("_COOKIE")))) {
15501549
ZVAL_DEREF(data);
15511550
if (Z_TYPE_P(data) == IS_ARRAY &&
1552-
(ppid = zend_hash_str_find(Z_ARRVAL_P(data), PS(session_name), strlen(PS(session_name))))) {
1551+
(ppid = zend_hash_find(Z_ARRVAL_P(data), PS(session_name)))) {
15531552
ZVAL_DEREF(ppid);
15541553
apply_trans_sid = 0;
15551554
}
15561555
}
15571556
}
15581557
if (apply_trans_sid) {
1559-
zend_string *sname;
1560-
sname = zend_string_init(PS(session_name), strlen(PS(session_name)), 0);
1561-
php_url_scanner_reset_session_var(sname, 1); /* This may fail when session name has changed */
1562-
zend_string_release_ex(sname, 0);
1563-
php_url_scanner_add_session_var(PS(session_name), strlen(PS(session_name)), ZSTR_VAL(PS(id)), ZSTR_LEN(PS(id)), 1);
1558+
php_url_scanner_reset_session_var(PS(session_name), true); /* This may fail when session name has changed */
1559+
php_url_scanner_add_session_var(ZSTR_VAL(PS(session_name)), ZSTR_LEN(PS(session_name)), ZSTR_VAL(PS(id)), ZSTR_LEN(PS(id)), true);
15641560
}
15651561
return SUCCESS;
15661562
}
@@ -1571,7 +1567,6 @@ PHPAPI zend_result php_session_start(void)
15711567
zval *ppid;
15721568
zval *data;
15731569
char *value;
1574-
size_t lensess;
15751570

15761571
switch (PS(session_status)) {
15771572
case php_session_active:
@@ -1606,8 +1601,6 @@ PHPAPI zend_result php_session_start(void)
16061601
PS(send_cookie) = PS(use_cookies) || PS(use_only_cookies);
16071602
}
16081603

1609-
lensess = strlen(PS(session_name));
1610-
16111604
/*
16121605
* Cookies are preferred, because initially cookie and get
16131606
* variables will be available.
@@ -1619,7 +1612,7 @@ PHPAPI zend_result php_session_start(void)
16191612
if (!PS(id)) {
16201613
if (PS(use_cookies) && (data = zend_hash_str_find(&EG(symbol_table), ZEND_STRL("_COOKIE")))) {
16211614
ZVAL_DEREF(data);
1622-
if (Z_TYPE_P(data) == IS_ARRAY && (ppid = zend_hash_str_find(Z_ARRVAL_P(data), PS(session_name), lensess))) {
1615+
if (Z_TYPE_P(data) == IS_ARRAY && (ppid = zend_hash_find(Z_ARRVAL_P(data), PS(session_name)))) {
16231616
ppid2sid(ppid);
16241617
PS(send_cookie) = 0;
16251618
PS(define_sid) = 0;
@@ -1629,13 +1622,13 @@ PHPAPI zend_result php_session_start(void)
16291622
if (!PS(use_only_cookies)) {
16301623
if (!PS(id) && (data = zend_hash_str_find(&EG(symbol_table), ZEND_STRL("_GET")))) {
16311624
ZVAL_DEREF(data);
1632-
if (Z_TYPE_P(data) == IS_ARRAY && (ppid = zend_hash_str_find(Z_ARRVAL_P(data), PS(session_name), lensess))) {
1625+
if (Z_TYPE_P(data) == IS_ARRAY && (ppid = zend_hash_find(Z_ARRVAL_P(data), PS(session_name)))) {
16331626
ppid2sid(ppid);
16341627
}
16351628
}
16361629
if (!PS(id) && (data = zend_hash_str_find(&EG(symbol_table), ZEND_STRL("_POST")))) {
16371630
ZVAL_DEREF(data);
1638-
if (Z_TYPE_P(data) == IS_ARRAY && (ppid = zend_hash_str_find(Z_ARRVAL_P(data), PS(session_name), lensess))) {
1631+
if (Z_TYPE_P(data) == IS_ARRAY && (ppid = zend_hash_find(Z_ARRVAL_P(data), PS(session_name)))) {
16391632
ppid2sid(ppid);
16401633
}
16411634
}
@@ -1717,7 +1710,7 @@ static zend_result php_session_reset(void)
17171710
PHPAPI void session_adapt_url(const char *url, size_t url_len, char **new_url, size_t *new_len)
17181711
{
17191712
if (APPLY_TRANS_SID && (PS(session_status) == php_session_active)) {
1720-
*new_url = php_url_scanner_adapt_single_url(url, url_len, PS(session_name), ZSTR_VAL(PS(id)), new_len, 1);
1713+
*new_url = php_url_scanner_adapt_single_url(url, url_len, ZSTR_VAL(PS(session_name)), ZSTR_VAL(PS(id)), new_len, true);
17211714
}
17221715
}
17231716

@@ -1913,7 +1906,6 @@ PHP_FUNCTION(session_get_cookie_params)
19131906
PHP_FUNCTION(session_name)
19141907
{
19151908
zend_string *name = NULL;
1916-
zend_string *ini_name;
19171909

19181910
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|P!", &name) == FAILURE) {
19191911
RETURN_THROWS();
@@ -1929,10 +1921,10 @@ PHP_FUNCTION(session_name)
19291921
RETURN_FALSE;
19301922
}
19311923

1932-
RETVAL_STRING(PS(session_name));
1924+
RETVAL_STRINGL(ZSTR_VAL(PS(session_name)), ZSTR_LEN(PS(session_name)));
19331925

19341926
if (name) {
1935-
ini_name = ZSTR_INIT_LITERAL("session.name", 0);
1927+
zend_string *ini_name = ZSTR_INIT_LITERAL("session.name", 0);
19361928
zend_alter_ini_entry(ini_name, name, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
19371929
zend_string_release_ex(ini_name, 0);
19381930
}
@@ -2338,7 +2330,7 @@ PHP_FUNCTION(session_regenerate_id)
23382330
zend_string_release_ex(PS(id), 0);
23392331
PS(id) = NULL;
23402332

2341-
if (PS(mod)->s_open(&PS(mod_data), ZSTR_VAL(PS(save_path)), PS(session_name)) == FAILURE) {
2333+
if (PS(mod)->s_open(&PS(mod_data), ZSTR_VAL(PS(save_path)), ZSTR_VAL(PS(session_name))) == FAILURE) {
23422334
PS(session_status) = php_session_none;
23432335
if (!EG(exception)) {
23442336
zend_throw_error(NULL, "Failed to open session: %s (path: %s)", PS(mod)->s_name, ZSTR_VAL(PS(save_path)));
@@ -3021,7 +3013,7 @@ static bool early_find_sid_in(zval *dest, int where, php_session_rfc1867_progres
30213013
return 0;
30223014
}
30233015

3024-
if ((ppid = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[where]), PS(session_name), progress->sname_len))
3016+
if ((ppid = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[where]), ZSTR_VAL(PS(session_name)), progress->sname_len))
30253017
&& Z_TYPE_P(ppid) == IS_STRING) {
30263018
zval_ptr_dtor(dest);
30273019
ZVAL_COPY_DEREF(dest, ppid);
@@ -3129,7 +3121,7 @@ static zend_result php_session_rfc1867_callback(unsigned int event, void *event_
31293121
multipart_event_start *data = (multipart_event_start *) event_data;
31303122
progress = ecalloc(1, sizeof(php_session_rfc1867_progress));
31313123
progress->content_length = data->content_length;
3132-
progress->sname_len = strlen(PS(session_name));
3124+
progress->sname_len = ZSTR_LEN(PS(session_name));
31333125
PS(rfc1867_progress) = progress;
31343126
}
31353127
break;
@@ -3151,7 +3143,7 @@ static zend_result php_session_rfc1867_callback(unsigned int event, void *event_
31513143
if (data->name && data->value && value_len) {
31523144
size_t name_len = strlen(data->name);
31533145

3154-
if (name_len == progress->sname_len && memcmp(data->name, PS(session_name), name_len) == 0) {
3146+
if (zend_string_equals_cstr(PS(session_name), data->name, name_len)) {
31553147
zval_ptr_dtor(&progress->sid);
31563148
ZVAL_STRINGL(&progress->sid, (*data->value), value_len);
31573149
} else if (zend_string_equals_cstr(PS(rfc1867_name), data->name, name_len)) {

0 commit comments

Comments
 (0)