Skip to content

Commit 8e93cb1

Browse files
Girgiasjorgsowa
authored andcommitted
Convert some Session globals from char* to zend_string*
However, the code is somewhat disgusting as I'm hitting Zend/zend_types.h:1222: zend_gc_delref: Assertion (zval_gc_flags((p)->u.type_info) & ((1<<7)|(1<<8))) != (1<<7) failed. Failures that I don't understand.
1 parent 79c71c9 commit 8e93cb1

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

ext/session/php_session.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,14 @@ typedef struct _php_ps_globals {
142142
char *save_path;
143143
char *session_name;
144144
zend_string *id;
145-
char *extern_referer_chk;
146-
char *cache_limiter;
145+
zend_string *extern_referer_chk;
146+
zend_string *cache_limiter;
147147
zend_long cookie_lifetime;
148-
char *cookie_path;
149-
char *cookie_domain;
150-
char *cookie_samesite;
148+
zend_string *cookie_path;
149+
zend_string *cookie_domain;
151150
bool cookie_secure;
152151
bool cookie_httponly;
152+
zend_string *cookie_samesite;
153153
const ps_module *mod;
154154
const ps_module *default_mod;
155155
void *mod_data;

ext/session/session.c

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -734,11 +734,14 @@ static PHP_INI_MH(OnUpdateSessionLong) /* {{{ */
734734
/* }}} */
735735

736736

737-
static PHP_INI_MH(OnUpdateSessionString) /* {{{ */
737+
static PHP_INI_MH(OnUpdateSessionStr) /* {{{ */
738738
{
739739
SESSION_CHECK_ACTIVE_STATE;
740740
SESSION_CHECK_OUTPUT_STATE;
741-
return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
741+
742+
zend_string **p = (zend_string **) ZEND_INI_GET_ADDR();
743+
*p = new_value ? new_value : NULL;
744+
return SUCCESS;
742745
}
743746
/* }}} */
744747

@@ -900,16 +903,16 @@ PHP_INI_BEGIN()
900903
STD_PHP_INI_ENTRY("session.gc_maxlifetime", "1440", PHP_INI_ALL, OnUpdateSessionLong, gc_maxlifetime, php_ps_globals, ps_globals)
901904
PHP_INI_ENTRY("session.serialize_handler", "php", PHP_INI_ALL, OnUpdateSerializer)
902905
STD_PHP_INI_ENTRY("session.cookie_lifetime", "0", PHP_INI_ALL, OnUpdateCookieLifetime,cookie_lifetime, php_ps_globals, ps_globals)
903-
STD_PHP_INI_ENTRY("session.cookie_path", "/", PHP_INI_ALL, OnUpdateSessionString, cookie_path, php_ps_globals, ps_globals)
904-
STD_PHP_INI_ENTRY("session.cookie_domain", "", PHP_INI_ALL, OnUpdateSessionString, cookie_domain, php_ps_globals, ps_globals)
906+
STD_PHP_INI_ENTRY("session.cookie_path", "/", PHP_INI_ALL, OnUpdateSessionStr, cookie_path, php_ps_globals, ps_globals)
907+
STD_PHP_INI_ENTRY("session.cookie_domain", "", PHP_INI_ALL, OnUpdateSessionStr, cookie_domain, php_ps_globals, ps_globals)
905908
STD_PHP_INI_BOOLEAN("session.cookie_secure", "0", PHP_INI_ALL, OnUpdateSessionBool, cookie_secure, php_ps_globals, ps_globals)
906909
STD_PHP_INI_BOOLEAN("session.cookie_httponly", "0", PHP_INI_ALL, OnUpdateSessionBool, cookie_httponly, php_ps_globals, ps_globals)
907-
STD_PHP_INI_ENTRY("session.cookie_samesite", "", PHP_INI_ALL, OnUpdateSessionString, cookie_samesite, php_ps_globals, ps_globals)
910+
STD_PHP_INI_ENTRY("session.cookie_samesite", "", PHP_INI_ALL, OnUpdateSessionStr, cookie_samesite, php_ps_globals, ps_globals)
908911
STD_PHP_INI_BOOLEAN("session.use_cookies", "1", PHP_INI_ALL, OnUpdateSessionBool, use_cookies, php_ps_globals, ps_globals)
909912
STD_PHP_INI_BOOLEAN("session.use_only_cookies", "1", PHP_INI_ALL, OnUpdateUseOnlyCookies, use_only_cookies, php_ps_globals, ps_globals)
910913
STD_PHP_INI_BOOLEAN("session.use_strict_mode", "0", PHP_INI_ALL, OnUpdateSessionBool, use_strict_mode, php_ps_globals, ps_globals)
911-
STD_PHP_INI_ENTRY("session.referer_check", "", PHP_INI_ALL, OnUpdateRefererCheck, extern_referer_chk, php_ps_globals, ps_globals)
912-
STD_PHP_INI_ENTRY("session.cache_limiter", "nocache", PHP_INI_ALL, OnUpdateSessionString, cache_limiter, php_ps_globals, ps_globals)
914+
STD_PHP_INI_ENTRY("session.referer_check", "", PHP_INI_ALL, OnUpdateSessionStr, extern_referer_chk, php_ps_globals, ps_globals)
915+
STD_PHP_INI_ENTRY("session.cache_limiter", "nocache", PHP_INI_ALL, OnUpdateSessionStr, cache_limiter, php_ps_globals, ps_globals)
913916
STD_PHP_INI_ENTRY("session.cache_expire", "180", PHP_INI_ALL, OnUpdateSessionLong, cache_expire, php_ps_globals, ps_globals)
914917
STD_PHP_INI_BOOLEAN("session.use_trans_sid", "0", PHP_INI_ALL, OnUpdateUseTransSid, use_trans_sid, php_ps_globals, ps_globals)
915918
PHP_INI_ENTRY("session.sid_length", "32", PHP_INI_ALL, OnUpdateSidLength)
@@ -1323,7 +1326,7 @@ static int php_session_cache_limiter(void) /* {{{ */
13231326
{
13241327
const php_session_cache_limiter_t *lim;
13251328

1326-
if (PS(cache_limiter)[0] == '\0') return 0;
1329+
if (!PS(cache_limiter) || ZSTR_LEN(PS(cache_limiter)) == 0) return 0;
13271330
if (PS(session_status) != php_session_active) return -1;
13281331

13291332
if (SG(headers_sent)) {
@@ -1340,7 +1343,8 @@ static int php_session_cache_limiter(void) /* {{{ */
13401343
}
13411344

13421345
for (lim = php_session_cache_limiters; lim->name; lim++) {
1343-
if (!strcasecmp(lim->name, PS(cache_limiter))) {
1346+
// TODO Use zend_string_cmp API?
1347+
if (!strcasecmp(lim->name, ZSTR_VAL(PS(cache_limiter)))) {
13441348
lim->func();
13451349
return 0;
13461350
}
@@ -1449,14 +1453,14 @@ static zend_result php_session_send_cookie(void) /* {{{ */
14491453
}
14501454
}
14511455

1452-
if (PS(cookie_path)[0]) {
1456+
if (PS(cookie_path) && ZSTR_LEN(PS(cookie_path)) != 0) {
14531457
smart_str_appends(&ncookie, COOKIE_PATH);
1454-
smart_str_appends(&ncookie, PS(cookie_path));
1458+
smart_str_append(&ncookie, PS(cookie_path));
14551459
}
14561460

1457-
if (PS(cookie_domain)[0]) {
1461+
if (PS(cookie_domain) && ZSTR_LEN(PS(cookie_domain)) != 0) {
14581462
smart_str_appends(&ncookie, COOKIE_DOMAIN);
1459-
smart_str_appends(&ncookie, PS(cookie_domain));
1463+
smart_str_append(&ncookie, PS(cookie_domain));
14601464
}
14611465

14621466
if (PS(cookie_secure)) {
@@ -1467,9 +1471,9 @@ static zend_result php_session_send_cookie(void) /* {{{ */
14671471
smart_str_appends(&ncookie, COOKIE_HTTPONLY);
14681472
}
14691473

1470-
if (PS(cookie_samesite)[0]) {
1474+
if (PS(cookie_samesite) && ZSTR_LEN(PS(cookie_samesite)) != 0) {
14711475
smart_str_appends(&ncookie, COOKIE_SAMESITE);
1472-
smart_str_appends(&ncookie, PS(cookie_samesite));
1476+
smart_str_append(&ncookie, PS(cookie_samesite));
14731477
}
14741478

14751479
smart_str_0(&ncookie);
@@ -1679,12 +1683,12 @@ PHPAPI zend_result php_session_start(void) /* {{{ */
16791683
}
16801684
/* Check whether the current request was referred to by
16811685
* an external site which invalidates the previously found id. */
1682-
if (PS(id) && PS(extern_referer_chk)[0] != '\0' &&
1686+
if (PS(id) && PS(extern_referer_chk) && ZSTR_LEN(PS(extern_referer_chk)) != 0 &&
16831687
!Z_ISUNDEF(PG(http_globals)[TRACK_VARS_SERVER]) &&
16841688
(data = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), "HTTP_REFERER", sizeof("HTTP_REFERER") - 1)) &&
16851689
Z_TYPE_P(data) == IS_STRING &&
16861690
Z_STRLEN_P(data) != 0 &&
1687-
strstr(Z_STRVAL_P(data), PS(extern_referer_chk)) == NULL
1691+
strstr(Z_STRVAL_P(data), ZSTR_VAL(PS(extern_referer_chk))) == NULL
16881692
) {
16891693
zend_string_release_ex(PS(id), 0);
16901694
PS(id) = NULL;
@@ -1949,11 +1953,15 @@ PHP_FUNCTION(session_get_cookie_params)
19491953
array_init(return_value);
19501954

19511955
add_assoc_long(return_value, "lifetime", PS(cookie_lifetime));
1952-
add_assoc_string(return_value, "path", PS(cookie_path));
1953-
add_assoc_string(return_value, "domain", PS(cookie_domain));
1956+
// TODO Use add_assoc_str() but figure out why it emits a
1957+
// Zend/zend_types.h:1222: zend_gc_delref: Assertion `(zval_gc_flags((p)->u.type_info) & ((1<<7)|(1<<8))) != (1<<7)' failed.
1958+
add_assoc_string(return_value, "path", ZSTR_VAL(PS(cookie_path)));
1959+
add_assoc_string(return_value, "domain", ZSTR_VAL(PS(cookie_domain)));
19541960
add_assoc_bool(return_value, "secure", PS(cookie_secure));
19551961
add_assoc_bool(return_value, "httponly", PS(cookie_httponly));
1956-
add_assoc_string(return_value, "samesite", PS(cookie_samesite));
1962+
// TODO Use add_assoc_str() but figure out why it emits a
1963+
// Zend/zend_types.h:1222: zend_gc_delref: Assertion `(zval_gc_flags((p)->u.type_info) & ((1<<7)|(1<<8))) != (1<<7)' failed.
1964+
add_assoc_string(return_value, "samesite", ZSTR_VAL(PS(cookie_samesite)));
19571965
}
19581966
/* }}} */
19591967

@@ -2530,7 +2538,12 @@ PHP_FUNCTION(session_cache_limiter)
25302538
RETURN_FALSE;
25312539
}
25322540

2533-
RETVAL_STRING(PS(cache_limiter));
2541+
zend_string *result_str = PS(cache_limiter);
2542+
if (!result_str) {
2543+
result_str = zend_empty_string;
2544+
}
2545+
// TODO Prevent duplication???
2546+
RETVAL_STR(zend_string_dup(result_str, false));
25342547

25352548
if (limiter) {
25362549
ini_name = ZSTR_INIT_LITERAL("session.cache_limiter", 0);

0 commit comments

Comments
 (0)